clock.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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), 1000);
  81. if (ret < 0)
  82. return ret;
  83. return buf;
  84. }
  85. static bool uac_clock_source_is_valid(struct snd_usb_audio *chip, int source_id)
  86. {
  87. int err;
  88. unsigned char data;
  89. struct usb_device *dev = chip->dev;
  90. struct uac_clock_source_descriptor *cs_desc =
  91. snd_usb_find_clock_source(chip->ctrl_intf, source_id);
  92. if (!cs_desc)
  93. return 0;
  94. /* If a clock source can't tell us whether it's valid, we assume it is */
  95. if (!uac2_control_is_readable(cs_desc->bmControls, UAC2_CS_CONTROL_CLOCK_VALID))
  96. return 1;
  97. err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_CUR,
  98. USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
  99. UAC2_CS_CONTROL_CLOCK_VALID << 8,
  100. snd_usb_ctrl_intf(chip) | (source_id << 8),
  101. &data, sizeof(data), 1000);
  102. if (err < 0) {
  103. snd_printk(KERN_WARNING "%s(): cannot get clock validity for id %d\n",
  104. __func__, source_id);
  105. return 0;
  106. }
  107. return !!data;
  108. }
  109. static int __uac_clock_find_source(struct snd_usb_audio *chip,
  110. int entity_id, unsigned long *visited)
  111. {
  112. struct uac_clock_source_descriptor *source;
  113. struct uac_clock_selector_descriptor *selector;
  114. struct uac_clock_multiplier_descriptor *multiplier;
  115. entity_id &= 0xff;
  116. if (test_and_set_bit(entity_id, visited)) {
  117. snd_printk(KERN_WARNING
  118. "%s(): recursive clock topology detected, id %d.\n",
  119. __func__, entity_id);
  120. return -EINVAL;
  121. }
  122. /* first, see if the ID we're looking for is a clock source already */
  123. source = snd_usb_find_clock_source(chip->ctrl_intf, entity_id);
  124. if (source)
  125. return source->bClockID;
  126. selector = snd_usb_find_clock_selector(chip->ctrl_intf, entity_id);
  127. if (selector) {
  128. int ret;
  129. /* the entity ID we are looking for is a selector.
  130. * find out what it currently selects */
  131. ret = uac_clock_selector_get_val(chip, selector->bClockID);
  132. if (ret < 0)
  133. return ret;
  134. /* Selector values are one-based */
  135. if (ret > selector->bNrInPins || ret < 1) {
  136. printk(KERN_ERR
  137. "%s(): selector reported illegal value, id %d, ret %d\n",
  138. __func__, selector->bClockID, ret);
  139. return -EINVAL;
  140. }
  141. return __uac_clock_find_source(chip, selector->baCSourceID[ret-1],
  142. visited);
  143. }
  144. /* FIXME: multipliers only act as pass-thru element for now */
  145. multiplier = snd_usb_find_clock_multiplier(chip->ctrl_intf, entity_id);
  146. if (multiplier)
  147. return __uac_clock_find_source(chip, multiplier->bCSourceID,
  148. visited);
  149. return -EINVAL;
  150. }
  151. /*
  152. * For all kinds of sample rate settings and other device queries,
  153. * the clock source (end-leaf) must be used. However, clock selectors,
  154. * clock multipliers and sample rate converters may be specified as
  155. * clock source input to terminal. This functions walks the clock path
  156. * to its end and tries to find the source.
  157. *
  158. * The 'visited' bitfield is used internally to detect recursive loops.
  159. *
  160. * Returns the clock source UnitID (>=0) on success, or an error.
  161. */
  162. int snd_usb_clock_find_source(struct snd_usb_audio *chip, int entity_id)
  163. {
  164. DECLARE_BITMAP(visited, 256);
  165. memset(visited, 0, sizeof(visited));
  166. return __uac_clock_find_source(chip, entity_id, visited);
  167. }
  168. static int set_sample_rate_v1(struct snd_usb_audio *chip, int iface,
  169. struct usb_host_interface *alts,
  170. struct audioformat *fmt, int rate)
  171. {
  172. struct usb_device *dev = chip->dev;
  173. unsigned int ep;
  174. unsigned char data[3];
  175. int err, crate;
  176. ep = get_endpoint(alts, 0)->bEndpointAddress;
  177. /* if endpoint doesn't have sampling rate control, bail out */
  178. if (!(fmt->attributes & UAC_EP_CS_ATTR_SAMPLE_RATE))
  179. return 0;
  180. data[0] = rate;
  181. data[1] = rate >> 8;
  182. data[2] = rate >> 16;
  183. if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR,
  184. USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_OUT,
  185. UAC_EP_CS_ATTR_SAMPLE_RATE << 8, ep,
  186. data, sizeof(data), 1000)) < 0) {
  187. snd_printk(KERN_ERR "%d:%d:%d: cannot set freq %d to ep %#x\n",
  188. dev->devnum, iface, fmt->altsetting, rate, ep);
  189. return err;
  190. }
  191. if ((err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC_GET_CUR,
  192. USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_IN,
  193. UAC_EP_CS_ATTR_SAMPLE_RATE << 8, ep,
  194. data, sizeof(data), 1000)) < 0) {
  195. snd_printk(KERN_WARNING "%d:%d:%d: cannot get freq at ep %#x\n",
  196. dev->devnum, iface, fmt->altsetting, ep);
  197. return 0; /* some devices don't support reading */
  198. }
  199. crate = data[0] | (data[1] << 8) | (data[2] << 16);
  200. if (crate != rate) {
  201. snd_printd(KERN_WARNING "current rate %d is different from the runtime rate %d\n", crate, rate);
  202. // runtime->rate = crate;
  203. }
  204. return 0;
  205. }
  206. static int set_sample_rate_v2(struct snd_usb_audio *chip, int iface,
  207. struct usb_host_interface *alts,
  208. struct audioformat *fmt, int rate)
  209. {
  210. struct usb_device *dev = chip->dev;
  211. unsigned char data[4];
  212. int err, crate;
  213. int clock = snd_usb_clock_find_source(chip, fmt->clock);
  214. if (clock < 0)
  215. return clock;
  216. if (!uac_clock_source_is_valid(chip, clock)) {
  217. /* TODO: should we try to find valid clock setups by ourself? */
  218. snd_printk(KERN_ERR "%d:%d:%d: clock source %d is not valid, cannot use\n",
  219. dev->devnum, iface, fmt->altsetting, clock);
  220. return -ENXIO;
  221. }
  222. data[0] = rate;
  223. data[1] = rate >> 8;
  224. data[2] = rate >> 16;
  225. data[3] = rate >> 24;
  226. if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC2_CS_CUR,
  227. USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
  228. UAC2_CS_CONTROL_SAM_FREQ << 8,
  229. snd_usb_ctrl_intf(chip) | (clock << 8),
  230. data, sizeof(data), 1000)) < 0) {
  231. snd_printk(KERN_ERR "%d:%d:%d: cannot set freq %d (v2)\n",
  232. dev->devnum, iface, fmt->altsetting, rate);
  233. return err;
  234. }
  235. if ((err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_CUR,
  236. USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
  237. UAC2_CS_CONTROL_SAM_FREQ << 8,
  238. snd_usb_ctrl_intf(chip) | (clock << 8),
  239. data, sizeof(data), 1000)) < 0) {
  240. snd_printk(KERN_WARNING "%d:%d:%d: cannot get freq (v2)\n",
  241. dev->devnum, iface, fmt->altsetting);
  242. return err;
  243. }
  244. crate = data[0] | (data[1] << 8) | (data[2] << 16) | (data[3] << 24);
  245. if (crate != rate)
  246. snd_printd(KERN_WARNING "current rate %d is different from the runtime rate %d\n", crate, rate);
  247. return 0;
  248. }
  249. int snd_usb_init_sample_rate(struct snd_usb_audio *chip, int iface,
  250. struct usb_host_interface *alts,
  251. struct audioformat *fmt, int rate)
  252. {
  253. struct usb_interface_descriptor *altsd = get_iface_desc(alts);
  254. switch (altsd->bInterfaceProtocol) {
  255. case UAC_VERSION_1:
  256. default:
  257. return set_sample_rate_v1(chip, iface, alts, fmt, rate);
  258. case UAC_VERSION_2:
  259. return set_sample_rate_v2(chip, iface, alts, fmt, rate);
  260. }
  261. }