clock.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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. snd_printk(KERN_ERR
  100. "usb-audio:%d: setting selector (id %d) unexpected length %d\n",
  101. chip->dev->devnum, 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. snd_printk(KERN_ERR
  109. "usb-audio:%d: setting selector (id %d) to %x failed (current: %d)\n",
  110. chip->dev->devnum, 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. snd_printk(KERN_WARNING "%s(): cannot get clock validity for id %d\n",
  135. __func__, source_id);
  136. return 0;
  137. }
  138. return !!data;
  139. }
  140. static int __uac_clock_find_source(struct snd_usb_audio *chip,
  141. int entity_id, unsigned long *visited,
  142. bool validate)
  143. {
  144. struct uac_clock_source_descriptor *source;
  145. struct uac_clock_selector_descriptor *selector;
  146. struct uac_clock_multiplier_descriptor *multiplier;
  147. entity_id &= 0xff;
  148. if (test_and_set_bit(entity_id, visited)) {
  149. snd_printk(KERN_WARNING
  150. "%s(): recursive clock topology detected, id %d.\n",
  151. __func__, entity_id);
  152. return -EINVAL;
  153. }
  154. /* first, see if the ID we're looking for is a clock source already */
  155. source = snd_usb_find_clock_source(chip->ctrl_intf, entity_id);
  156. if (source) {
  157. entity_id = source->bClockID;
  158. if (validate && !uac_clock_source_is_valid(chip, entity_id)) {
  159. snd_printk(KERN_ERR "usb-audio:%d: clock source %d is not valid, cannot use\n",
  160. chip->dev->devnum, entity_id);
  161. return -ENXIO;
  162. }
  163. return entity_id;
  164. }
  165. selector = snd_usb_find_clock_selector(chip->ctrl_intf, entity_id);
  166. if (selector) {
  167. int ret, i, cur;
  168. /* the entity ID we are looking for is a selector.
  169. * find out what it currently selects */
  170. ret = uac_clock_selector_get_val(chip, selector->bClockID);
  171. if (ret < 0)
  172. return ret;
  173. /* Selector values are one-based */
  174. if (ret > selector->bNrInPins || ret < 1) {
  175. snd_printk(KERN_ERR
  176. "%s(): selector reported illegal value, id %d, ret %d\n",
  177. __func__, selector->bClockID, ret);
  178. return -EINVAL;
  179. }
  180. cur = ret;
  181. ret = __uac_clock_find_source(chip, selector->baCSourceID[ret - 1],
  182. visited, validate);
  183. if (!validate || ret > 0 || !chip->autoclock)
  184. return ret;
  185. /* The current clock source is invalid, try others. */
  186. for (i = 1; i <= selector->bNrInPins; i++) {
  187. int err;
  188. if (i == cur)
  189. continue;
  190. ret = __uac_clock_find_source(chip, selector->baCSourceID[i - 1],
  191. visited, true);
  192. if (ret < 0)
  193. continue;
  194. err = uac_clock_selector_set_val(chip, entity_id, i);
  195. if (err < 0)
  196. continue;
  197. snd_printk(KERN_INFO
  198. "usb-audio:%d: found and selected valid clock source %d\n",
  199. chip->dev->devnum, ret);
  200. return ret;
  201. }
  202. return -ENXIO;
  203. }
  204. /* FIXME: multipliers only act as pass-thru element for now */
  205. multiplier = snd_usb_find_clock_multiplier(chip->ctrl_intf, entity_id);
  206. if (multiplier)
  207. return __uac_clock_find_source(chip, multiplier->bCSourceID,
  208. visited, validate);
  209. return -EINVAL;
  210. }
  211. /*
  212. * For all kinds of sample rate settings and other device queries,
  213. * the clock source (end-leaf) must be used. However, clock selectors,
  214. * clock multipliers and sample rate converters may be specified as
  215. * clock source input to terminal. This functions walks the clock path
  216. * to its end and tries to find the source.
  217. *
  218. * The 'visited' bitfield is used internally to detect recursive loops.
  219. *
  220. * Returns the clock source UnitID (>=0) on success, or an error.
  221. */
  222. int snd_usb_clock_find_source(struct snd_usb_audio *chip, int entity_id,
  223. bool validate)
  224. {
  225. DECLARE_BITMAP(visited, 256);
  226. memset(visited, 0, sizeof(visited));
  227. return __uac_clock_find_source(chip, entity_id, visited, validate);
  228. }
  229. static int set_sample_rate_v1(struct snd_usb_audio *chip, int iface,
  230. struct usb_host_interface *alts,
  231. struct audioformat *fmt, int rate)
  232. {
  233. struct usb_device *dev = chip->dev;
  234. unsigned int ep;
  235. unsigned char data[3];
  236. int err, crate;
  237. ep = get_endpoint(alts, 0)->bEndpointAddress;
  238. /* if endpoint doesn't have sampling rate control, bail out */
  239. if (!(fmt->attributes & UAC_EP_CS_ATTR_SAMPLE_RATE))
  240. return 0;
  241. data[0] = rate;
  242. data[1] = rate >> 8;
  243. data[2] = rate >> 16;
  244. if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR,
  245. USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_OUT,
  246. UAC_EP_CS_ATTR_SAMPLE_RATE << 8, ep,
  247. data, sizeof(data))) < 0) {
  248. snd_printk(KERN_ERR "%d:%d:%d: cannot set freq %d to ep %#x\n",
  249. dev->devnum, iface, fmt->altsetting, rate, ep);
  250. return err;
  251. }
  252. if ((err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC_GET_CUR,
  253. USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_IN,
  254. UAC_EP_CS_ATTR_SAMPLE_RATE << 8, ep,
  255. data, sizeof(data))) < 0) {
  256. snd_printk(KERN_WARNING "%d:%d:%d: cannot get freq at ep %#x\n",
  257. dev->devnum, iface, fmt->altsetting, ep);
  258. return 0; /* some devices don't support reading */
  259. }
  260. crate = data[0] | (data[1] << 8) | (data[2] << 16);
  261. if (crate != rate) {
  262. snd_printd(KERN_WARNING "current rate %d is different from the runtime rate %d\n", crate, rate);
  263. // runtime->rate = crate;
  264. }
  265. return 0;
  266. }
  267. static int get_sample_rate_v2(struct snd_usb_audio *chip, int iface,
  268. int altsetting, int clock)
  269. {
  270. struct usb_device *dev = chip->dev;
  271. __le32 data;
  272. int err;
  273. err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_CUR,
  274. USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
  275. UAC2_CS_CONTROL_SAM_FREQ << 8,
  276. snd_usb_ctrl_intf(chip) | (clock << 8),
  277. &data, sizeof(data));
  278. if (err < 0) {
  279. snd_printk(KERN_WARNING "%d:%d:%d: cannot get freq (v2): err %d\n",
  280. dev->devnum, iface, altsetting, err);
  281. return 0;
  282. }
  283. return le32_to_cpu(data);
  284. }
  285. static int set_sample_rate_v2(struct snd_usb_audio *chip, int iface,
  286. struct usb_host_interface *alts,
  287. struct audioformat *fmt, int rate)
  288. {
  289. struct usb_device *dev = chip->dev;
  290. __le32 data;
  291. int err, cur_rate, prev_rate;
  292. int clock;
  293. bool writeable;
  294. struct uac_clock_source_descriptor *cs_desc;
  295. clock = snd_usb_clock_find_source(chip, fmt->clock, true);
  296. if (clock < 0)
  297. return clock;
  298. prev_rate = get_sample_rate_v2(chip, iface, fmt->altsetting, clock);
  299. if (prev_rate == rate)
  300. return 0;
  301. cs_desc = snd_usb_find_clock_source(chip->ctrl_intf, clock);
  302. writeable = uac2_control_is_writeable(cs_desc->bmControls, UAC2_CS_CONTROL_SAM_FREQ - 1);
  303. if (writeable) {
  304. data = cpu_to_le32(rate);
  305. err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC2_CS_CUR,
  306. USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
  307. UAC2_CS_CONTROL_SAM_FREQ << 8,
  308. snd_usb_ctrl_intf(chip) | (clock << 8),
  309. &data, sizeof(data));
  310. if (err < 0) {
  311. snd_printk(KERN_ERR "%d:%d:%d: cannot set freq %d (v2): err %d\n",
  312. dev->devnum, iface, fmt->altsetting, rate, err);
  313. return err;
  314. }
  315. cur_rate = get_sample_rate_v2(chip, iface, fmt->altsetting, clock);
  316. } else {
  317. cur_rate = prev_rate;
  318. }
  319. if (cur_rate != rate) {
  320. if (!writeable) {
  321. snd_printk(KERN_WARNING
  322. "%d:%d:%d: freq mismatch (RO clock): req %d, clock runs @%d\n",
  323. dev->devnum, iface, fmt->altsetting, rate, cur_rate);
  324. return -ENXIO;
  325. }
  326. snd_printd(KERN_WARNING
  327. "current rate %d is different from the runtime rate %d\n",
  328. cur_rate, rate);
  329. }
  330. /* Some devices doesn't respond to sample rate changes while the
  331. * interface is active. */
  332. if (rate != prev_rate) {
  333. usb_set_interface(dev, iface, 0);
  334. snd_usb_set_interface_quirk(dev);
  335. usb_set_interface(dev, iface, fmt->altsetting);
  336. snd_usb_set_interface_quirk(dev);
  337. }
  338. return 0;
  339. }
  340. int snd_usb_init_sample_rate(struct snd_usb_audio *chip, int iface,
  341. struct usb_host_interface *alts,
  342. struct audioformat *fmt, int rate)
  343. {
  344. switch (fmt->protocol) {
  345. case UAC_VERSION_1:
  346. default:
  347. return set_sample_rate_v1(chip, iface, alts, fmt, rate);
  348. case UAC_VERSION_2:
  349. return set_sample_rate_v2(chip, iface, alts, fmt, rate);
  350. }
  351. }