ALSA: usb-audio: UAC2: try to find and switch to valid clock
[linux-2.6-block.git] / sound / usb / clock.c
CommitLineData
79f920fb
DM
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
20#include <linux/bitops.h>
21#include <linux/init.h>
79f920fb
DM
22#include <linux/string.h>
23#include <linux/usb.h>
79f920fb
DM
24#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>
79f920fb
DM
30
31#include "usbaudio.h"
32#include "card.h"
79f920fb 33#include "helper.h"
f22aa949 34#include "clock.h"
79f920fb
DM
35
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,
11bcbc44
DM
92 UAC2_CX_CLOCK_SELECTOR << 8,
93 snd_usb_ctrl_intf(chip) | (selector_id << 8),
17d900c4 94 &buf, sizeof(buf));
79f920fb
DM
95
96 if (ret < 0)
97 return ret;
98
99 return buf;
100}
101
8c55af3f
EZ
102static 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
79f920fb
DM
137static 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;
3bc6fbc7
DM
142 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 */
aff252a8
DM
149 if (!uac2_control_is_readable(cs_desc->bmControls,
150 UAC2_CS_CONTROL_CLOCK_VALID - 1))
3bc6fbc7 151 return 1;
79f920fb
DM
152
153 err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_CUR,
154 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
11bcbc44
DM
155 UAC2_CS_CONTROL_CLOCK_VALID << 8,
156 snd_usb_ctrl_intf(chip) | (source_id << 8),
17d900c4 157 &data, sizeof(data));
79f920fb
DM
158
159 if (err < 0) {
160 snd_printk(KERN_WARNING "%s(): cannot get clock validity for id %d\n",
161 __func__, source_id);
3bc6fbc7 162 return 0;
79f920fb
DM
163 }
164
165 return !!data;
166}
167
79f920fb 168static int __uac_clock_find_source(struct snd_usb_audio *chip,
06ffc1eb
EZ
169 int entity_id, unsigned long *visited,
170 bool validate)
79f920fb
DM
171{
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 */
3d8d4dcf 186 source = snd_usb_find_clock_source(chip->ctrl_intf, entity_id);
06ffc1eb
EZ
187 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 }
79f920fb 196
3d8d4dcf 197 selector = snd_usb_find_clock_selector(chip->ctrl_intf, entity_id);
79f920fb 198 if (selector) {
8c55af3f 199 int ret, i, cur;
79f920fb
DM
200
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
157a57b6
DM
207 /* Selector values are one-based */
208
79f920fb 209 if (ret > selector->bNrInPins || ret < 1) {
06ffc1eb 210 snd_printk(KERN_ERR
79f920fb
DM
211 "%s(): selector reported illegal value, id %d, ret %d\n",
212 __func__, selector->bClockID, ret);
213
214 return -EINVAL;
215 }
216
8c55af3f
EZ
217 cur = ret;
218 ret = __uac_clock_find_source(chip, selector->baCSourceID[ret - 1],
06ffc1eb 219 visited, validate);
8c55af3f
EZ
220 if (!validate || ret > 0)
221 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;
79f920fb
DM
246 }
247
248 /* FIXME: multipliers only act as pass-thru element for now */
3d8d4dcf 249 multiplier = snd_usb_find_clock_multiplier(chip->ctrl_intf, entity_id);
79f920fb 250 if (multiplier)
3d8d4dcf 251 return __uac_clock_find_source(chip, multiplier->bCSourceID,
06ffc1eb 252 visited, validate);
79f920fb
DM
253
254 return -EINVAL;
255}
256
157a57b6
DM
257/*
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 */
06ffc1eb
EZ
268int snd_usb_clock_find_source(struct snd_usb_audio *chip, int entity_id,
269 bool validate)
79f920fb
DM
270{
271 DECLARE_BITMAP(visited, 256);
272 memset(visited, 0, sizeof(visited));
06ffc1eb 273 return __uac_clock_find_source(chip, entity_id, visited, validate);
79f920fb
DM
274}
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 */
d32d552e 288 if (!(fmt->attributes & UAC_EP_CS_ATTR_SAMPLE_RATE))
79f920fb 289 return 0;
79f920fb
DM
290
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,
17d900c4 297 data, sizeof(data))) < 0) {
79f920fb
DM
298 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,
17d900c4 306 data, sizeof(data))) < 0) {
79f920fb
DM
307 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
7c517465
TI
321static 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;
f6a8bc70 325 __le32 data;
7c517465
TI
326 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),
f6a8bc70 332 &data, sizeof(data));
7c517465
TI
333 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
f6a8bc70 339 return le32_to_cpu(data);
7c517465
TI
340}
341
79f920fb
DM
342static 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;
f6a8bc70 347 __le32 data;
690a863f 348 int err, cur_rate, prev_rate;
8c55af3f 349 int clock;
79f920fb 350
8c55af3f 351 clock = snd_usb_clock_find_source(chip, fmt->clock, true);
79f920fb
DM
352 if (clock < 0)
353 return clock;
354
7c517465 355 prev_rate = get_sample_rate_v2(chip, iface, fmt->altsetting, clock);
690a863f 356
f6a8bc70 357 data = cpu_to_le32(rate);
79f920fb
DM
358 if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC2_CS_CUR,
359 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
11bcbc44
DM
360 UAC2_CS_CONTROL_SAM_FREQ << 8,
361 snd_usb_ctrl_intf(chip) | (clock << 8),
f6a8bc70 362 &data, sizeof(data))) < 0) {
79f920fb
DM
363 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
7c517465 368 cur_rate = get_sample_rate_v2(chip, iface, fmt->altsetting, clock);
79f920fb 369
690a863f
TH
370 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 }
79f920fb
DM
382
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:
a2acad82 394 default:
79f920fb
DM
395 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 }
79f920fb
DM
400}
401