blob: e59d359b907ccf72ca7ec0e42f11ad3a1b8ff5a3 [file] [log] [blame]
Daniel Mack79f920f2010-05-31 14:51:31 +02001/*
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
20#include <linux/bitops.h>
21#include <linux/init.h>
Daniel Mack79f920f2010-05-31 14:51:31 +020022#include <linux/string.h>
23#include <linux/usb.h>
Daniel Mack79f920f2010-05-31 14:51:31 +020024#include <linux/usb/audio.h>
25#include <linux/usb/audio-v2.h>
26
27#include <sound/core.h>
28#include <sound/info.h>
29#include <sound/pcm.h>
Daniel Mack79f920f2010-05-31 14:51:31 +020030
31#include "usbaudio.h"
32#include "card.h"
Daniel Mack79f920f2010-05-31 14:51:31 +020033#include "helper.h"
Daniel Mackf22aa942010-06-16 17:57:27 +020034#include "clock.h"
Daniel Mack79f920f2010-05-31 14:51:31 +020035
36static struct uac_clock_source_descriptor *
37 snd_usb_find_clock_source(struct usb_host_interface *ctrl_iface,
38 int clock_id)
39{
40 struct uac_clock_source_descriptor *cs = NULL;
41
42 while ((cs = snd_usb_find_csint_desc(ctrl_iface->extra,
43 ctrl_iface->extralen,
44 cs, UAC2_CLOCK_SOURCE))) {
45 if (cs->bClockID == clock_id)
46 return cs;
47 }
48
49 return NULL;
50}
51
52static struct uac_clock_selector_descriptor *
53 snd_usb_find_clock_selector(struct usb_host_interface *ctrl_iface,
54 int clock_id)
55{
56 struct uac_clock_selector_descriptor *cs = NULL;
57
58 while ((cs = snd_usb_find_csint_desc(ctrl_iface->extra,
59 ctrl_iface->extralen,
60 cs, UAC2_CLOCK_SELECTOR))) {
61 if (cs->bClockID == clock_id)
62 return cs;
63 }
64
65 return NULL;
66}
67
68static struct uac_clock_multiplier_descriptor *
69 snd_usb_find_clock_multiplier(struct usb_host_interface *ctrl_iface,
70 int clock_id)
71{
72 struct uac_clock_multiplier_descriptor *cs = NULL;
73
74 while ((cs = snd_usb_find_csint_desc(ctrl_iface->extra,
75 ctrl_iface->extralen,
76 cs, UAC2_CLOCK_MULTIPLIER))) {
77 if (cs->bClockID == clock_id)
78 return cs;
79 }
80
81 return NULL;
82}
83
84static int uac_clock_selector_get_val(struct snd_usb_audio *chip, int selector_id)
85{
86 unsigned char buf;
87 int ret;
88
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,
Daniel Mack11bcbc42010-06-11 17:34:20 +020092 UAC2_CX_CLOCK_SELECTOR << 8,
93 snd_usb_ctrl_intf(chip) | (selector_id << 8),
Clemens Ladisch17d900c2011-09-26 21:15:27 +020094 &buf, sizeof(buf));
Daniel Mack79f920f2010-05-31 14:51:31 +020095
96 if (ret < 0)
97 return ret;
98
99 return buf;
100}
101
Eldad Zack8c55af32013-04-03 23:18:55 +0200102static int uac_clock_selector_set_val(struct snd_usb_audio *chip, int selector_id,
103 unsigned char pin)
104{
105 int ret;
106
107 ret = snd_usb_ctl_msg(chip->dev, usb_sndctrlpipe(chip->dev, 0),
108 UAC2_CS_CUR,
109 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
110 UAC2_CX_CLOCK_SELECTOR << 8,
111 snd_usb_ctrl_intf(chip) | (selector_id << 8),
112 &pin, sizeof(pin));
113 if (ret < 0)
114 return ret;
115
116 if (ret != sizeof(pin)) {
117 snd_printk(KERN_ERR
118 "usb-audio:%d: setting selector (id %d) unexpected length %d\n",
119 chip->dev->devnum, selector_id, ret);
120 return -EINVAL;
121 }
122
123 ret = uac_clock_selector_get_val(chip, selector_id);
124 if (ret < 0)
125 return ret;
126
127 if (ret != pin) {
128 snd_printk(KERN_ERR
129 "usb-audio:%d: setting selector (id %d) to %x failed (current: %d)\n",
130 chip->dev->devnum, selector_id, pin, ret);
131 return -EINVAL;
132 }
133
134 return ret;
135}
136
Daniel Mack79f920f2010-05-31 14:51:31 +0200137static bool uac_clock_source_is_valid(struct snd_usb_audio *chip, int source_id)
138{
139 int err;
140 unsigned char data;
141 struct usb_device *dev = chip->dev;
Daniel Mack3bc6fbc72011-05-18 11:28:44 +0200142 struct uac_clock_source_descriptor *cs_desc =
143 snd_usb_find_clock_source(chip->ctrl_intf, source_id);
144
145 if (!cs_desc)
146 return 0;
147
148 /* If a clock source can't tell us whether it's valid, we assume it is */
Daniel Mackaff252a2012-08-01 10:16:53 +0200149 if (!uac2_control_is_readable(cs_desc->bmControls,
150 UAC2_CS_CONTROL_CLOCK_VALID - 1))
Daniel Mack3bc6fbc72011-05-18 11:28:44 +0200151 return 1;
Daniel Mack79f920f2010-05-31 14:51:31 +0200152
153 err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_CUR,
154 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
Daniel Mack11bcbc42010-06-11 17:34:20 +0200155 UAC2_CS_CONTROL_CLOCK_VALID << 8,
156 snd_usb_ctrl_intf(chip) | (source_id << 8),
Clemens Ladisch17d900c2011-09-26 21:15:27 +0200157 &data, sizeof(data));
Daniel Mack79f920f2010-05-31 14:51:31 +0200158
159 if (err < 0) {
160 snd_printk(KERN_WARNING "%s(): cannot get clock validity for id %d\n",
161 __func__, source_id);
Daniel Mack3bc6fbc72011-05-18 11:28:44 +0200162 return 0;
Daniel Mack79f920f2010-05-31 14:51:31 +0200163 }
164
165 return !!data;
166}
167
Daniel Mack79f920f2010-05-31 14:51:31 +0200168static int __uac_clock_find_source(struct snd_usb_audio *chip,
Eldad Zack06ffc1e2013-04-03 23:18:54 +0200169 int entity_id, unsigned long *visited,
170 bool validate)
Daniel Mack79f920f2010-05-31 14:51:31 +0200171{
172 struct uac_clock_source_descriptor *source;
173 struct uac_clock_selector_descriptor *selector;
174 struct uac_clock_multiplier_descriptor *multiplier;
175
176 entity_id &= 0xff;
177
178 if (test_and_set_bit(entity_id, visited)) {
179 snd_printk(KERN_WARNING
180 "%s(): recursive clock topology detected, id %d.\n",
181 __func__, entity_id);
182 return -EINVAL;
183 }
184
185 /* first, see if the ID we're looking for is a clock source already */
Daniel Mack3d8d4dc2010-06-16 17:57:31 +0200186 source = snd_usb_find_clock_source(chip->ctrl_intf, entity_id);
Eldad Zack06ffc1e2013-04-03 23:18:54 +0200187 if (source) {
188 entity_id = source->bClockID;
189 if (validate && !uac_clock_source_is_valid(chip, entity_id)) {
190 snd_printk(KERN_ERR "usb-audio:%d: clock source %d is not valid, cannot use\n",
191 chip->dev->devnum, entity_id);
192 return -ENXIO;
193 }
194 return entity_id;
195 }
Daniel Mack79f920f2010-05-31 14:51:31 +0200196
Daniel Mack3d8d4dc2010-06-16 17:57:31 +0200197 selector = snd_usb_find_clock_selector(chip->ctrl_intf, entity_id);
Daniel Mack79f920f2010-05-31 14:51:31 +0200198 if (selector) {
Eldad Zack8c55af32013-04-03 23:18:55 +0200199 int ret, i, cur;
Daniel Mack79f920f2010-05-31 14:51:31 +0200200
201 /* the entity ID we are looking for is a selector.
202 * find out what it currently selects */
203 ret = uac_clock_selector_get_val(chip, selector->bClockID);
204 if (ret < 0)
205 return ret;
206
Daniel Mack157a57b2010-06-16 17:57:30 +0200207 /* Selector values are one-based */
208
Daniel Mack79f920f2010-05-31 14:51:31 +0200209 if (ret > selector->bNrInPins || ret < 1) {
Eldad Zack06ffc1e2013-04-03 23:18:54 +0200210 snd_printk(KERN_ERR
Daniel Mack79f920f2010-05-31 14:51:31 +0200211 "%s(): selector reported illegal value, id %d, ret %d\n",
212 __func__, selector->bClockID, ret);
213
214 return -EINVAL;
215 }
216
Eldad Zack8c55af32013-04-03 23:18:55 +0200217 cur = ret;
218 ret = __uac_clock_find_source(chip, selector->baCSourceID[ret - 1],
Eldad Zack06ffc1e2013-04-03 23:18:54 +0200219 visited, validate);
Eldad Zackef02e292013-04-03 23:18:56 +0200220 if (!validate || ret > 0 || !chip->autoclock)
Eldad Zack8c55af32013-04-03 23:18:55 +0200221 return ret;
222
223 /* The current clock source is invalid, try others. */
224 for (i = 1; i <= selector->bNrInPins; i++) {
225 int err;
226
227 if (i == cur)
228 continue;
229
230 ret = __uac_clock_find_source(chip, selector->baCSourceID[i - 1],
231 visited, true);
232 if (ret < 0)
233 continue;
234
235 err = uac_clock_selector_set_val(chip, entity_id, i);
236 if (err < 0)
237 continue;
238
239 snd_printk(KERN_INFO
240 "usb-audio:%d: found and selected valid clock source %d\n",
241 chip->dev->devnum, ret);
242 return ret;
243 }
244
245 return -ENXIO;
Daniel Mack79f920f2010-05-31 14:51:31 +0200246 }
247
248 /* FIXME: multipliers only act as pass-thru element for now */
Daniel Mack3d8d4dc2010-06-16 17:57:31 +0200249 multiplier = snd_usb_find_clock_multiplier(chip->ctrl_intf, entity_id);
Daniel Mack79f920f2010-05-31 14:51:31 +0200250 if (multiplier)
Daniel Mack3d8d4dc2010-06-16 17:57:31 +0200251 return __uac_clock_find_source(chip, multiplier->bCSourceID,
Eldad Zack06ffc1e2013-04-03 23:18:54 +0200252 visited, validate);
Daniel Mack79f920f2010-05-31 14:51:31 +0200253
254 return -EINVAL;
255}
256
Daniel Mack157a57b2010-06-16 17:57:30 +0200257/*
258 * For all kinds of sample rate settings and other device queries,
259 * the clock source (end-leaf) must be used. However, clock selectors,
260 * clock multipliers and sample rate converters may be specified as
261 * clock source input to terminal. This functions walks the clock path
262 * to its end and tries to find the source.
263 *
264 * The 'visited' bitfield is used internally to detect recursive loops.
265 *
266 * Returns the clock source UnitID (>=0) on success, or an error.
267 */
Eldad Zack06ffc1e2013-04-03 23:18:54 +0200268int snd_usb_clock_find_source(struct snd_usb_audio *chip, int entity_id,
269 bool validate)
Daniel Mack79f920f2010-05-31 14:51:31 +0200270{
271 DECLARE_BITMAP(visited, 256);
272 memset(visited, 0, sizeof(visited));
Eldad Zack06ffc1e2013-04-03 23:18:54 +0200273 return __uac_clock_find_source(chip, entity_id, visited, validate);
Daniel Mack79f920f2010-05-31 14:51:31 +0200274}
275
276static int set_sample_rate_v1(struct snd_usb_audio *chip, int iface,
277 struct usb_host_interface *alts,
278 struct audioformat *fmt, int rate)
279{
280 struct usb_device *dev = chip->dev;
281 unsigned int ep;
282 unsigned char data[3];
283 int err, crate;
284
285 ep = get_endpoint(alts, 0)->bEndpointAddress;
286
287 /* if endpoint doesn't have sampling rate control, bail out */
Clemens Ladischd32d5522010-07-08 16:38:01 +0200288 if (!(fmt->attributes & UAC_EP_CS_ATTR_SAMPLE_RATE))
Daniel Mack79f920f2010-05-31 14:51:31 +0200289 return 0;
Daniel Mack79f920f2010-05-31 14:51:31 +0200290
291 data[0] = rate;
292 data[1] = rate >> 8;
293 data[2] = rate >> 16;
294 if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR,
295 USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_OUT,
296 UAC_EP_CS_ATTR_SAMPLE_RATE << 8, ep,
Clemens Ladisch17d900c2011-09-26 21:15:27 +0200297 data, sizeof(data))) < 0) {
Daniel Mack79f920f2010-05-31 14:51:31 +0200298 snd_printk(KERN_ERR "%d:%d:%d: cannot set freq %d to ep %#x\n",
299 dev->devnum, iface, fmt->altsetting, rate, ep);
300 return err;
301 }
302
303 if ((err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC_GET_CUR,
304 USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_IN,
305 UAC_EP_CS_ATTR_SAMPLE_RATE << 8, ep,
Clemens Ladisch17d900c2011-09-26 21:15:27 +0200306 data, sizeof(data))) < 0) {
Daniel Mack79f920f2010-05-31 14:51:31 +0200307 snd_printk(KERN_WARNING "%d:%d:%d: cannot get freq at ep %#x\n",
308 dev->devnum, iface, fmt->altsetting, ep);
309 return 0; /* some devices don't support reading */
310 }
311
312 crate = data[0] | (data[1] << 8) | (data[2] << 16);
313 if (crate != rate) {
314 snd_printd(KERN_WARNING "current rate %d is different from the runtime rate %d\n", crate, rate);
315 // runtime->rate = crate;
316 }
317
318 return 0;
319}
320
Takashi Iwai7c5174652013-04-03 19:08:29 +0200321static int get_sample_rate_v2(struct snd_usb_audio *chip, int iface,
322 int altsetting, int clock)
323{
324 struct usb_device *dev = chip->dev;
Eldad Zackf6a8bc72013-04-03 23:18:53 +0200325 __le32 data;
Takashi Iwai7c5174652013-04-03 19:08:29 +0200326 int err;
327
328 err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_CUR,
329 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
330 UAC2_CS_CONTROL_SAM_FREQ << 8,
331 snd_usb_ctrl_intf(chip) | (clock << 8),
Eldad Zackf6a8bc72013-04-03 23:18:53 +0200332 &data, sizeof(data));
Takashi Iwai7c5174652013-04-03 19:08:29 +0200333 if (err < 0) {
334 snd_printk(KERN_WARNING "%d:%d:%d: cannot get freq (v2)\n",
335 dev->devnum, iface, altsetting);
336 return 0;
337 }
338
Eldad Zackf6a8bc72013-04-03 23:18:53 +0200339 return le32_to_cpu(data);
Takashi Iwai7c5174652013-04-03 19:08:29 +0200340}
341
Daniel Mack79f920f2010-05-31 14:51:31 +0200342static int set_sample_rate_v2(struct snd_usb_audio *chip, int iface,
343 struct usb_host_interface *alts,
344 struct audioformat *fmt, int rate)
345{
346 struct usb_device *dev = chip->dev;
Eldad Zackf6a8bc72013-04-03 23:18:53 +0200347 __le32 data;
Torstein Hegge690a8632013-03-26 22:10:05 +0100348 int err, cur_rate, prev_rate;
Eldad Zack8c55af32013-04-03 23:18:55 +0200349 int clock;
Daniel Mack79f920f2010-05-31 14:51:31 +0200350
Eldad Zack8c55af32013-04-03 23:18:55 +0200351 clock = snd_usb_clock_find_source(chip, fmt->clock, true);
Daniel Mack79f920f2010-05-31 14:51:31 +0200352 if (clock < 0)
353 return clock;
354
Takashi Iwai7c5174652013-04-03 19:08:29 +0200355 prev_rate = get_sample_rate_v2(chip, iface, fmt->altsetting, clock);
Torstein Hegge690a8632013-03-26 22:10:05 +0100356
Eldad Zackf6a8bc72013-04-03 23:18:53 +0200357 data = cpu_to_le32(rate);
Daniel Mack79f920f2010-05-31 14:51:31 +0200358 if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC2_CS_CUR,
359 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
Daniel Mack11bcbc42010-06-11 17:34:20 +0200360 UAC2_CS_CONTROL_SAM_FREQ << 8,
361 snd_usb_ctrl_intf(chip) | (clock << 8),
Eldad Zackf6a8bc72013-04-03 23:18:53 +0200362 &data, sizeof(data))) < 0) {
Daniel Mack79f920f2010-05-31 14:51:31 +0200363 snd_printk(KERN_ERR "%d:%d:%d: cannot set freq %d (v2)\n",
364 dev->devnum, iface, fmt->altsetting, rate);
365 return err;
366 }
367
Takashi Iwai7c5174652013-04-03 19:08:29 +0200368 cur_rate = get_sample_rate_v2(chip, iface, fmt->altsetting, clock);
Daniel Mack79f920f2010-05-31 14:51:31 +0200369
Torstein Hegge690a8632013-03-26 22:10:05 +0100370 if (cur_rate != rate) {
371 snd_printd(KERN_WARNING
372 "current rate %d is different from the runtime rate %d\n",
373 cur_rate, rate);
374 }
375
376 /* Some devices doesn't respond to sample rate changes while the
377 * interface is active. */
378 if (rate != prev_rate) {
379 usb_set_interface(dev, iface, 0);
380 usb_set_interface(dev, iface, fmt->altsetting);
381 }
Daniel Mack79f920f2010-05-31 14:51:31 +0200382
383 return 0;
384}
385
386int snd_usb_init_sample_rate(struct snd_usb_audio *chip, int iface,
387 struct usb_host_interface *alts,
388 struct audioformat *fmt, int rate)
389{
390 struct usb_interface_descriptor *altsd = get_iface_desc(alts);
391
392 switch (altsd->bInterfaceProtocol) {
393 case UAC_VERSION_1:
Clemens Ladischa2acad82010-09-03 10:53:11 +0200394 default:
Daniel Mack79f920f2010-05-31 14:51:31 +0200395 return set_sample_rate_v1(chip, iface, alts, fmt, rate);
396
397 case UAC_VERSION_2:
398 return set_sample_rate_v2(chip, iface, alts, fmt, rate);
399 }
Daniel Mack79f920f2010-05-31 14:51:31 +0200400}
401