clock.c 12 KB

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