clock.c 8.9 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/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,
  93. snd_usb_ctrl_intf(chip) | (selector_id << 8),
  94. &buf, sizeof(buf), 1000);
  95. if (ret < 0)
  96. return ret;
  97. return buf;
  98. }
  99. static bool uac_clock_source_is_valid(struct snd_usb_audio *chip, int source_id)
  100. {
  101. int err;
  102. unsigned char data;
  103. struct usb_device *dev = chip->dev;
  104. err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_CUR,
  105. USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
  106. UAC2_CS_CONTROL_CLOCK_VALID << 8,
  107. snd_usb_ctrl_intf(chip) | (source_id << 8),
  108. &data, sizeof(data), 1000);
  109. if (err < 0) {
  110. snd_printk(KERN_WARNING "%s(): cannot get clock validity for id %d\n",
  111. __func__, source_id);
  112. return err;
  113. }
  114. return !!data;
  115. }
  116. /* Try to find the clock source ID of a given clock entity */
  117. static int __uac_clock_find_source(struct snd_usb_audio *chip,
  118. struct usb_host_interface *host_iface,
  119. int entity_id, unsigned long *visited)
  120. {
  121. struct uac_clock_source_descriptor *source;
  122. struct uac_clock_selector_descriptor *selector;
  123. struct uac_clock_multiplier_descriptor *multiplier;
  124. entity_id &= 0xff;
  125. if (test_and_set_bit(entity_id, visited)) {
  126. snd_printk(KERN_WARNING
  127. "%s(): recursive clock topology detected, id %d.\n",
  128. __func__, entity_id);
  129. return -EINVAL;
  130. }
  131. /* first, see if the ID we're looking for is a clock source already */
  132. source = snd_usb_find_clock_source(host_iface, entity_id);
  133. if (source)
  134. return source->bClockID;
  135. selector = snd_usb_find_clock_selector(host_iface, entity_id);
  136. if (selector) {
  137. int ret;
  138. /* the entity ID we are looking for is a selector.
  139. * find out what it currently selects */
  140. ret = uac_clock_selector_get_val(chip, selector->bClockID);
  141. if (ret < 0)
  142. return ret;
  143. if (ret > selector->bNrInPins || ret < 1) {
  144. printk(KERN_ERR
  145. "%s(): selector reported illegal value, id %d, ret %d\n",
  146. __func__, selector->bClockID, ret);
  147. return -EINVAL;
  148. }
  149. return __uac_clock_find_source(chip, host_iface,
  150. selector->baCSourceID[ret-1],
  151. visited);
  152. }
  153. /* FIXME: multipliers only act as pass-thru element for now */
  154. multiplier = snd_usb_find_clock_multiplier(host_iface, entity_id);
  155. if (multiplier)
  156. return __uac_clock_find_source(chip, host_iface,
  157. multiplier->bCSourceID, visited);
  158. return -EINVAL;
  159. }
  160. int snd_usb_clock_find_source(struct snd_usb_audio *chip,
  161. struct usb_host_interface *host_iface,
  162. int entity_id)
  163. {
  164. DECLARE_BITMAP(visited, 256);
  165. memset(visited, 0, sizeof(visited));
  166. return __uac_clock_find_source(chip, host_iface, 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. snd_printk(KERN_WARNING "%d:%d:%d: endpoint lacks sample rate attribute bit, cannot set.\n",
  180. dev->devnum, iface, fmt->altsetting);
  181. return 0;
  182. }
  183. data[0] = rate;
  184. data[1] = rate >> 8;
  185. data[2] = rate >> 16;
  186. if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR,
  187. USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_OUT,
  188. UAC_EP_CS_ATTR_SAMPLE_RATE << 8, ep,
  189. data, sizeof(data), 1000)) < 0) {
  190. snd_printk(KERN_ERR "%d:%d:%d: cannot set freq %d to ep %#x\n",
  191. dev->devnum, iface, fmt->altsetting, rate, ep);
  192. return err;
  193. }
  194. if ((err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC_GET_CUR,
  195. USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_IN,
  196. UAC_EP_CS_ATTR_SAMPLE_RATE << 8, ep,
  197. data, sizeof(data), 1000)) < 0) {
  198. snd_printk(KERN_WARNING "%d:%d:%d: cannot get freq at ep %#x\n",
  199. dev->devnum, iface, fmt->altsetting, ep);
  200. return 0; /* some devices don't support reading */
  201. }
  202. crate = data[0] | (data[1] << 8) | (data[2] << 16);
  203. if (crate != rate) {
  204. snd_printd(KERN_WARNING "current rate %d is different from the runtime rate %d\n", crate, rate);
  205. // runtime->rate = crate;
  206. }
  207. return 0;
  208. }
  209. static int set_sample_rate_v2(struct snd_usb_audio *chip, int iface,
  210. struct usb_host_interface *alts,
  211. struct audioformat *fmt, int rate)
  212. {
  213. struct usb_device *dev = chip->dev;
  214. unsigned char data[4];
  215. int err, crate;
  216. int clock = snd_usb_clock_find_source(chip, chip->ctrl_intf, fmt->clock);
  217. if (clock < 0)
  218. return clock;
  219. if (!uac_clock_source_is_valid(chip, clock)) {
  220. snd_printk(KERN_ERR "%d:%d:%d: clock source %d is not valid, cannot use\n",
  221. dev->devnum, iface, fmt->altsetting, clock);
  222. return -ENXIO;
  223. }
  224. data[0] = rate;
  225. data[1] = rate >> 8;
  226. data[2] = rate >> 16;
  227. data[3] = rate >> 24;
  228. if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC2_CS_CUR,
  229. USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
  230. UAC2_CS_CONTROL_SAM_FREQ << 8,
  231. snd_usb_ctrl_intf(chip) | (clock << 8),
  232. data, sizeof(data), 1000)) < 0) {
  233. snd_printk(KERN_ERR "%d:%d:%d: cannot set freq %d (v2)\n",
  234. dev->devnum, iface, fmt->altsetting, rate);
  235. return err;
  236. }
  237. if ((err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_CUR,
  238. USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
  239. UAC2_CS_CONTROL_SAM_FREQ << 8,
  240. snd_usb_ctrl_intf(chip) | (clock << 8),
  241. data, sizeof(data), 1000)) < 0) {
  242. snd_printk(KERN_WARNING "%d:%d:%d: cannot get freq (v2)\n",
  243. dev->devnum, iface, fmt->altsetting);
  244. return err;
  245. }
  246. crate = data[0] | (data[1] << 8) | (data[2] << 16) | (data[3] << 24);
  247. if (crate != rate)
  248. snd_printd(KERN_WARNING "current rate %d is different from the runtime rate %d\n", crate, rate);
  249. return 0;
  250. }
  251. int snd_usb_init_sample_rate(struct snd_usb_audio *chip, int iface,
  252. struct usb_host_interface *alts,
  253. struct audioformat *fmt, int rate)
  254. {
  255. struct usb_interface_descriptor *altsd = get_iface_desc(alts);
  256. switch (altsd->bInterfaceProtocol) {
  257. case UAC_VERSION_1:
  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. return -EINVAL;
  263. }