clock.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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 <sound/core.h>
  26. #include <sound/info.h>
  27. #include <sound/pcm.h>
  28. #include "usbaudio.h"
  29. #include "card.h"
  30. #include "helper.h"
  31. #include "clock.h"
  32. #include "quirks.h"
  33. static struct uac_clock_source_descriptor *
  34. snd_usb_find_clock_source(struct usb_host_interface *ctrl_iface,
  35. int clock_id)
  36. {
  37. struct uac_clock_source_descriptor *cs = NULL;
  38. while ((cs = snd_usb_find_csint_desc(ctrl_iface->extra,
  39. ctrl_iface->extralen,
  40. cs, UAC2_CLOCK_SOURCE))) {
  41. if (cs->bLength >= sizeof(*cs) && cs->bClockID == clock_id)
  42. return cs;
  43. }
  44. return NULL;
  45. }
  46. static struct uac_clock_selector_descriptor *
  47. snd_usb_find_clock_selector(struct usb_host_interface *ctrl_iface,
  48. int clock_id)
  49. {
  50. struct uac_clock_selector_descriptor *cs = NULL;
  51. while ((cs = snd_usb_find_csint_desc(ctrl_iface->extra,
  52. ctrl_iface->extralen,
  53. cs, UAC2_CLOCK_SELECTOR))) {
  54. if (cs->bLength >= sizeof(*cs) && cs->bClockID == clock_id) {
  55. if (cs->bLength < 5 + cs->bNrInPins)
  56. return NULL;
  57. return cs;
  58. }
  59. }
  60. return NULL;
  61. }
  62. static struct uac_clock_multiplier_descriptor *
  63. snd_usb_find_clock_multiplier(struct usb_host_interface *ctrl_iface,
  64. int clock_id)
  65. {
  66. struct uac_clock_multiplier_descriptor *cs = NULL;
  67. while ((cs = snd_usb_find_csint_desc(ctrl_iface->extra,
  68. ctrl_iface->extralen,
  69. cs, UAC2_CLOCK_MULTIPLIER))) {
  70. if (cs->bLength >= sizeof(*cs) && cs->bClockID == clock_id)
  71. return cs;
  72. }
  73. return NULL;
  74. }
  75. static int uac_clock_selector_get_val(struct snd_usb_audio *chip, int selector_id)
  76. {
  77. unsigned char buf;
  78. int ret;
  79. ret = snd_usb_ctl_msg(chip->dev, usb_rcvctrlpipe(chip->dev, 0),
  80. UAC2_CS_CUR,
  81. USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
  82. UAC2_CX_CLOCK_SELECTOR << 8,
  83. snd_usb_ctrl_intf(chip) | (selector_id << 8),
  84. &buf, sizeof(buf));
  85. if (ret < 0)
  86. return ret;
  87. return buf;
  88. }
  89. static int uac_clock_selector_set_val(struct snd_usb_audio *chip, int selector_id,
  90. unsigned char pin)
  91. {
  92. int ret;
  93. ret = snd_usb_ctl_msg(chip->dev, usb_sndctrlpipe(chip->dev, 0),
  94. UAC2_CS_CUR,
  95. USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
  96. UAC2_CX_CLOCK_SELECTOR << 8,
  97. snd_usb_ctrl_intf(chip) | (selector_id << 8),
  98. &pin, sizeof(pin));
  99. if (ret < 0)
  100. return ret;
  101. if (ret != sizeof(pin)) {
  102. usb_audio_err(chip,
  103. "setting selector (id %d) unexpected length %d\n",
  104. selector_id, ret);
  105. return -EINVAL;
  106. }
  107. ret = uac_clock_selector_get_val(chip, selector_id);
  108. if (ret < 0)
  109. return ret;
  110. if (ret != pin) {
  111. usb_audio_err(chip,
  112. "setting selector (id %d) to %x failed (current: %d)\n",
  113. selector_id, pin, ret);
  114. return -EINVAL;
  115. }
  116. return ret;
  117. }
  118. static bool uac_clock_source_is_valid(struct snd_usb_audio *chip, int source_id)
  119. {
  120. int err;
  121. unsigned char data;
  122. struct usb_device *dev = chip->dev;
  123. struct uac_clock_source_descriptor *cs_desc =
  124. snd_usb_find_clock_source(chip->ctrl_intf, source_id);
  125. if (!cs_desc)
  126. return 0;
  127. /* If a clock source can't tell us whether it's valid, we assume it is */
  128. if (!uac2_control_is_readable(cs_desc->bmControls,
  129. UAC2_CS_CONTROL_CLOCK_VALID - 1))
  130. return 1;
  131. err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_CUR,
  132. USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
  133. UAC2_CS_CONTROL_CLOCK_VALID << 8,
  134. snd_usb_ctrl_intf(chip) | (source_id << 8),
  135. &data, sizeof(data));
  136. if (err < 0) {
  137. dev_warn(&dev->dev,
  138. "%s(): cannot get clock validity for id %d\n",
  139. __func__, source_id);
  140. return 0;
  141. }
  142. return !!data;
  143. }
  144. static int __uac_clock_find_source(struct snd_usb_audio *chip,
  145. int entity_id, unsigned long *visited,
  146. bool validate)
  147. {
  148. struct uac_clock_source_descriptor *source;
  149. struct uac_clock_selector_descriptor *selector;
  150. struct uac_clock_multiplier_descriptor *multiplier;
  151. entity_id &= 0xff;
  152. if (test_and_set_bit(entity_id, visited)) {
  153. usb_audio_warn(chip,
  154. "%s(): recursive clock topology detected, id %d.\n",
  155. __func__, entity_id);
  156. return -EINVAL;
  157. }
  158. /* first, see if the ID we're looking for is a clock source already */
  159. source = snd_usb_find_clock_source(chip->ctrl_intf, entity_id);
  160. if (source) {
  161. entity_id = source->bClockID;
  162. if (validate && !uac_clock_source_is_valid(chip, entity_id)) {
  163. usb_audio_err(chip,
  164. "clock source %d is not valid, cannot use\n",
  165. entity_id);
  166. return -ENXIO;
  167. }
  168. return entity_id;
  169. }
  170. selector = snd_usb_find_clock_selector(chip->ctrl_intf, entity_id);
  171. if (selector) {
  172. int ret, i, cur;
  173. /* the entity ID we are looking for is a selector.
  174. * find out what it currently selects */
  175. ret = uac_clock_selector_get_val(chip, selector->bClockID);
  176. if (ret < 0)
  177. return ret;
  178. /* Selector values are one-based */
  179. if (ret > selector->bNrInPins || ret < 1) {
  180. usb_audio_err(chip,
  181. "%s(): selector reported illegal value, id %d, ret %d\n",
  182. __func__, selector->bClockID, ret);
  183. return -EINVAL;
  184. }
  185. cur = ret;
  186. ret = __uac_clock_find_source(chip, selector->baCSourceID[ret - 1],
  187. visited, validate);
  188. if (!validate || ret > 0 || !chip->autoclock)
  189. return ret;
  190. /* The current clock source is invalid, try others. */
  191. for (i = 1; i <= selector->bNrInPins; i++) {
  192. int err;
  193. if (i == cur)
  194. continue;
  195. ret = __uac_clock_find_source(chip, selector->baCSourceID[i - 1],
  196. visited, true);
  197. if (ret < 0)
  198. continue;
  199. err = uac_clock_selector_set_val(chip, entity_id, i);
  200. if (err < 0)
  201. continue;
  202. usb_audio_info(chip,
  203. "found and selected valid clock source %d\n",
  204. ret);
  205. return ret;
  206. }
  207. return -ENXIO;
  208. }
  209. /* FIXME: multipliers only act as pass-thru element for now */
  210. multiplier = snd_usb_find_clock_multiplier(chip->ctrl_intf, entity_id);
  211. if (multiplier)
  212. return __uac_clock_find_source(chip, multiplier->bCSourceID,
  213. visited, validate);
  214. return -EINVAL;
  215. }
  216. /*
  217. * For all kinds of sample rate settings and other device queries,
  218. * the clock source (end-leaf) must be used. However, clock selectors,
  219. * clock multipliers and sample rate converters may be specified as
  220. * clock source input to terminal. This functions walks the clock path
  221. * to its end and tries to find the source.
  222. *
  223. * The 'visited' bitfield is used internally to detect recursive loops.
  224. *
  225. * Returns the clock source UnitID (>=0) on success, or an error.
  226. */
  227. int snd_usb_clock_find_source(struct snd_usb_audio *chip, int entity_id,
  228. bool validate)
  229. {
  230. DECLARE_BITMAP(visited, 256);
  231. memset(visited, 0, sizeof(visited));
  232. return __uac_clock_find_source(chip, entity_id, visited, validate);
  233. }
  234. static int set_sample_rate_v1(struct snd_usb_audio *chip, int iface,
  235. struct usb_host_interface *alts,
  236. struct audioformat *fmt, int rate)
  237. {
  238. struct usb_device *dev = chip->dev;
  239. unsigned int ep;
  240. unsigned char data[3];
  241. int err, crate;
  242. if (get_iface_desc(alts)->bNumEndpoints < 1)
  243. return -EINVAL;
  244. ep = get_endpoint(alts, 0)->bEndpointAddress;
  245. /* if endpoint doesn't have sampling rate control, bail out */
  246. if (!(fmt->attributes & UAC_EP_CS_ATTR_SAMPLE_RATE))
  247. return 0;
  248. data[0] = rate;
  249. data[1] = rate >> 8;
  250. data[2] = rate >> 16;
  251. if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR,
  252. USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_OUT,
  253. UAC_EP_CS_ATTR_SAMPLE_RATE << 8, ep,
  254. data, sizeof(data))) < 0) {
  255. dev_err(&dev->dev, "%d:%d: cannot set freq %d to ep %#x\n",
  256. iface, fmt->altsetting, rate, ep);
  257. return err;
  258. }
  259. /* Don't check the sample rate for devices which we know don't
  260. * support reading */
  261. if (snd_usb_get_sample_rate_quirk(chip))
  262. return 0;
  263. /* the firmware is likely buggy, don't repeat to fail too many times */
  264. if (chip->sample_rate_read_error > 2)
  265. return 0;
  266. if ((err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC_GET_CUR,
  267. USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_IN,
  268. UAC_EP_CS_ATTR_SAMPLE_RATE << 8, ep,
  269. data, sizeof(data))) < 0) {
  270. dev_err(&dev->dev, "%d:%d: cannot get freq at ep %#x\n",
  271. iface, fmt->altsetting, ep);
  272. chip->sample_rate_read_error++;
  273. return 0; /* some devices don't support reading */
  274. }
  275. crate = data[0] | (data[1] << 8) | (data[2] << 16);
  276. if (crate != rate) {
  277. dev_warn(&dev->dev, "current rate %d is different from the runtime rate %d\n", crate, rate);
  278. // runtime->rate = crate;
  279. }
  280. return 0;
  281. }
  282. static int get_sample_rate_v2(struct snd_usb_audio *chip, int iface,
  283. int altsetting, int clock)
  284. {
  285. struct usb_device *dev = chip->dev;
  286. __le32 data;
  287. int err;
  288. err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_CUR,
  289. USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
  290. UAC2_CS_CONTROL_SAM_FREQ << 8,
  291. snd_usb_ctrl_intf(chip) | (clock << 8),
  292. &data, sizeof(data));
  293. if (err < 0) {
  294. dev_warn(&dev->dev, "%d:%d: cannot get freq (v2): err %d\n",
  295. iface, altsetting, err);
  296. return 0;
  297. }
  298. return le32_to_cpu(data);
  299. }
  300. static int set_sample_rate_v2(struct snd_usb_audio *chip, int iface,
  301. struct usb_host_interface *alts,
  302. struct audioformat *fmt, int rate)
  303. {
  304. struct usb_device *dev = chip->dev;
  305. __le32 data;
  306. int err, cur_rate, prev_rate;
  307. int clock;
  308. bool writeable;
  309. struct uac_clock_source_descriptor *cs_desc;
  310. clock = snd_usb_clock_find_source(chip, fmt->clock, true);
  311. if (clock < 0)
  312. return clock;
  313. prev_rate = get_sample_rate_v2(chip, iface, fmt->altsetting, clock);
  314. if (prev_rate == rate)
  315. return 0;
  316. cs_desc = snd_usb_find_clock_source(chip->ctrl_intf, clock);
  317. writeable = uac2_control_is_writeable(cs_desc->bmControls, UAC2_CS_CONTROL_SAM_FREQ - 1);
  318. if (writeable) {
  319. data = cpu_to_le32(rate);
  320. err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC2_CS_CUR,
  321. USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
  322. UAC2_CS_CONTROL_SAM_FREQ << 8,
  323. snd_usb_ctrl_intf(chip) | (clock << 8),
  324. &data, sizeof(data));
  325. if (err < 0) {
  326. usb_audio_err(chip,
  327. "%d:%d: cannot set freq %d (v2): err %d\n",
  328. iface, fmt->altsetting, rate, err);
  329. return err;
  330. }
  331. cur_rate = get_sample_rate_v2(chip, iface, fmt->altsetting, clock);
  332. } else {
  333. cur_rate = prev_rate;
  334. }
  335. if (cur_rate != rate) {
  336. if (!writeable) {
  337. usb_audio_warn(chip,
  338. "%d:%d: freq mismatch (RO clock): req %d, clock runs @%d\n",
  339. iface, fmt->altsetting, rate, cur_rate);
  340. return -ENXIO;
  341. }
  342. usb_audio_dbg(chip,
  343. "current rate %d is different from the runtime rate %d\n",
  344. cur_rate, rate);
  345. }
  346. /* Some devices doesn't respond to sample rate changes while the
  347. * interface is active. */
  348. if (rate != prev_rate) {
  349. usb_set_interface(dev, iface, 0);
  350. snd_usb_set_interface_quirk(dev);
  351. usb_set_interface(dev, iface, fmt->altsetting);
  352. snd_usb_set_interface_quirk(dev);
  353. }
  354. return 0;
  355. }
  356. int snd_usb_init_sample_rate(struct snd_usb_audio *chip, int iface,
  357. struct usb_host_interface *alts,
  358. struct audioformat *fmt, int rate)
  359. {
  360. switch (fmt->protocol) {
  361. case UAC_VERSION_1:
  362. default:
  363. return set_sample_rate_v1(chip, iface, alts, fmt, rate);
  364. case UAC_VERSION_2:
  365. return set_sample_rate_v2(chip, iface, alts, fmt, rate);
  366. }
  367. }