clock.c 11 KB

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