clock.c 9.0 KB

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