clock.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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/list.h>
  22. #include <linux/slab.h>
  23. #include <linux/string.h>
  24. #include <linux/usb.h>
  25. #include <linux/moduleparam.h>
  26. #include <linux/mutex.h>
  27. #include <linux/usb/audio.h>
  28. #include <linux/usb/audio-v2.h>
  29. #include <sound/core.h>
  30. #include <sound/info.h>
  31. #include <sound/pcm.h>
  32. #include <sound/pcm_params.h>
  33. #include <sound/initval.h>
  34. #include "usbaudio.h"
  35. #include "card.h"
  36. #include "midi.h"
  37. #include "mixer.h"
  38. #include "proc.h"
  39. #include "quirks.h"
  40. #include "endpoint.h"
  41. #include "helper.h"
  42. #include "debug.h"
  43. #include "pcm.h"
  44. #include "urb.h"
  45. #include "format.h"
  46. static struct uac_clock_source_descriptor *
  47. snd_usb_find_clock_source(struct usb_host_interface *ctrl_iface,
  48. int clock_id)
  49. {
  50. struct uac_clock_source_descriptor *cs = NULL;
  51. while ((cs = snd_usb_find_csint_desc(ctrl_iface->extra,
  52. ctrl_iface->extralen,
  53. cs, UAC2_CLOCK_SOURCE))) {
  54. if (cs->bClockID == clock_id)
  55. return cs;
  56. }
  57. return NULL;
  58. }
  59. static struct uac_clock_selector_descriptor *
  60. snd_usb_find_clock_selector(struct usb_host_interface *ctrl_iface,
  61. int clock_id)
  62. {
  63. struct uac_clock_selector_descriptor *cs = NULL;
  64. while ((cs = snd_usb_find_csint_desc(ctrl_iface->extra,
  65. ctrl_iface->extralen,
  66. cs, UAC2_CLOCK_SELECTOR))) {
  67. if (cs->bClockID == clock_id)
  68. return cs;
  69. }
  70. return NULL;
  71. }
  72. static struct uac_clock_multiplier_descriptor *
  73. snd_usb_find_clock_multiplier(struct usb_host_interface *ctrl_iface,
  74. int clock_id)
  75. {
  76. struct uac_clock_multiplier_descriptor *cs = NULL;
  77. while ((cs = snd_usb_find_csint_desc(ctrl_iface->extra,
  78. ctrl_iface->extralen,
  79. cs, UAC2_CLOCK_MULTIPLIER))) {
  80. if (cs->bClockID == clock_id)
  81. return cs;
  82. }
  83. return NULL;
  84. }
  85. static int uac_clock_selector_get_val(struct snd_usb_audio *chip, int selector_id)
  86. {
  87. unsigned char buf;
  88. int ret;
  89. ret = snd_usb_ctl_msg(chip->dev, usb_rcvctrlpipe(chip->dev, 0),
  90. UAC2_CS_CUR,
  91. USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
  92. UAC2_CX_CLOCK_SELECTOR << 8, selector_id << 8,
  93. &buf, sizeof(buf), 1000);
  94. if (ret < 0)
  95. return ret;
  96. return buf;
  97. }
  98. static bool uac_clock_source_is_valid(struct snd_usb_audio *chip, int source_id)
  99. {
  100. int err;
  101. unsigned char data;
  102. struct usb_device *dev = chip->dev;
  103. err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_CUR,
  104. USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
  105. UAC2_CS_CONTROL_CLOCK_VALID << 8, source_id << 8,
  106. &data, sizeof(data), 1000);
  107. if (err < 0) {
  108. snd_printk(KERN_WARNING "%s(): cannot get clock validity for id %d\n",
  109. __func__, source_id);
  110. return err;
  111. }
  112. return !!data;
  113. }
  114. /* Try to find the clock source ID of a given clock entity */
  115. static int __uac_clock_find_source(struct snd_usb_audio *chip,
  116. struct usb_host_interface *host_iface,
  117. int entity_id, unsigned long *visited)
  118. {
  119. struct uac_clock_source_descriptor *source;
  120. struct uac_clock_selector_descriptor *selector;
  121. struct uac_clock_multiplier_descriptor *multiplier;
  122. entity_id &= 0xff;
  123. if (test_and_set_bit(entity_id, visited)) {
  124. snd_printk(KERN_WARNING
  125. "%s(): recursive clock topology detected, id %d.\n",
  126. __func__, entity_id);
  127. return -EINVAL;
  128. }
  129. /* first, see if the ID we're looking for is a clock source already */
  130. source = snd_usb_find_clock_source(host_iface, entity_id);
  131. if (source)
  132. return source->bClockID;
  133. selector = snd_usb_find_clock_selector(host_iface, entity_id);
  134. if (selector) {
  135. int ret;
  136. /* the entity ID we are looking for is a selector.
  137. * find out what it currently selects */
  138. ret = uac_clock_selector_get_val(chip, selector->bClockID);
  139. if (ret < 0)
  140. return ret;
  141. if (ret > selector->bNrInPins || ret < 1) {
  142. printk(KERN_ERR
  143. "%s(): selector reported illegal value, id %d, ret %d\n",
  144. __func__, selector->bClockID, ret);
  145. return -EINVAL;
  146. }
  147. return __uac_clock_find_source(chip, host_iface,
  148. selector->baCSourceID[ret-1],
  149. visited);
  150. }
  151. /* FIXME: multipliers only act as pass-thru element for now */
  152. multiplier = snd_usb_find_clock_multiplier(host_iface, entity_id);
  153. if (multiplier)
  154. return __uac_clock_find_source(chip, host_iface,
  155. multiplier->bCSourceID, visited);
  156. return -EINVAL;
  157. }
  158. int snd_usb_clock_find_source(struct snd_usb_audio *chip,
  159. struct usb_host_interface *host_iface,
  160. int entity_id)
  161. {
  162. DECLARE_BITMAP(visited, 256);
  163. memset(visited, 0, sizeof(visited));
  164. return __uac_clock_find_source(chip, host_iface, entity_id, visited);
  165. }
  166. static int set_sample_rate_v1(struct snd_usb_audio *chip, int iface,
  167. struct usb_host_interface *alts,
  168. struct audioformat *fmt, int rate)
  169. {
  170. struct usb_device *dev = chip->dev;
  171. unsigned int ep;
  172. unsigned char data[3];
  173. int err, crate;
  174. ep = get_endpoint(alts, 0)->bEndpointAddress;
  175. /* if endpoint doesn't have sampling rate control, bail out */
  176. if (!(fmt->attributes & UAC_EP_CS_ATTR_SAMPLE_RATE)) {
  177. snd_printk(KERN_WARNING "%d:%d:%d: endpoint lacks sample rate attribute bit, cannot set.\n",
  178. dev->devnum, iface, fmt->altsetting);
  179. return 0;
  180. }
  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), 1000)) < 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), 1000)) < 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, chip->ctrl_intf, fmt->clock);
  215. if (clock < 0)
  216. return clock;
  217. if (!uac_clock_source_is_valid(chip, clock)) {
  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, clock << 8,
  229. data, sizeof(data), 1000)) < 0) {
  230. snd_printk(KERN_ERR "%d:%d:%d: cannot set freq %d (v2)\n",
  231. dev->devnum, iface, fmt->altsetting, rate);
  232. return err;
  233. }
  234. if ((err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_CUR,
  235. USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
  236. UAC2_CS_CONTROL_SAM_FREQ << 8, clock << 8,
  237. data, sizeof(data), 1000)) < 0) {
  238. snd_printk(KERN_WARNING "%d:%d:%d: cannot get freq (v2)\n",
  239. dev->devnum, iface, fmt->altsetting);
  240. return err;
  241. }
  242. crate = data[0] | (data[1] << 8) | (data[2] << 16) | (data[3] << 24);
  243. if (crate != rate)
  244. snd_printd(KERN_WARNING "current rate %d is different from the runtime rate %d\n", crate, rate);
  245. return 0;
  246. }
  247. int snd_usb_init_sample_rate(struct snd_usb_audio *chip, int iface,
  248. struct usb_host_interface *alts,
  249. struct audioformat *fmt, int rate)
  250. {
  251. struct usb_interface_descriptor *altsd = get_iface_desc(alts);
  252. switch (altsd->bInterfaceProtocol) {
  253. case UAC_VERSION_1:
  254. return set_sample_rate_v1(chip, iface, alts, fmt, rate);
  255. case UAC_VERSION_2:
  256. return set_sample_rate_v2(chip, iface, alts, fmt, rate);
  257. }
  258. return -EINVAL;
  259. }