2 * Clock domain and sample rate management functions
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.
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.
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
20 #include <linux/bitops.h>
21 #include <linux/init.h>
22 #include <linux/string.h>
23 #include <linux/usb.h>
24 #include <linux/usb/audio.h>
25 #include <linux/usb/audio-v2.h>
27 #include <sound/core.h>
28 #include <sound/info.h>
29 #include <sound/pcm.h>
36 static struct uac_clock_source_descriptor
*
37 snd_usb_find_clock_source(struct usb_host_interface
*ctrl_iface
,
40 struct uac_clock_source_descriptor
*cs
= NULL
;
42 while ((cs
= snd_usb_find_csint_desc(ctrl_iface
->extra
,
44 cs
, UAC2_CLOCK_SOURCE
))) {
45 if (cs
->bClockID
== clock_id
)
52 static struct uac_clock_selector_descriptor
*
53 snd_usb_find_clock_selector(struct usb_host_interface
*ctrl_iface
,
56 struct uac_clock_selector_descriptor
*cs
= NULL
;
58 while ((cs
= snd_usb_find_csint_desc(ctrl_iface
->extra
,
60 cs
, UAC2_CLOCK_SELECTOR
))) {
61 if (cs
->bClockID
== clock_id
)
68 static struct uac_clock_multiplier_descriptor
*
69 snd_usb_find_clock_multiplier(struct usb_host_interface
*ctrl_iface
,
72 struct uac_clock_multiplier_descriptor
*cs
= NULL
;
74 while ((cs
= snd_usb_find_csint_desc(ctrl_iface
->extra
,
76 cs
, UAC2_CLOCK_MULTIPLIER
))) {
77 if (cs
->bClockID
== clock_id
)
84 static int uac_clock_selector_get_val(struct snd_usb_audio
*chip
, int selector_id
)
89 ret
= snd_usb_ctl_msg(chip
->dev
, usb_rcvctrlpipe(chip
->dev
, 0),
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),
102 static int uac_clock_selector_set_val(struct snd_usb_audio
*chip
, int selector_id
,
107 ret
= snd_usb_ctl_msg(chip
->dev
, usb_sndctrlpipe(chip
->dev
, 0),
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),
116 if (ret
!= sizeof(pin
)) {
118 "usb-audio:%d: setting selector (id %d) unexpected length %d\n",
119 chip
->dev
->devnum
, selector_id
, ret
);
123 ret
= uac_clock_selector_get_val(chip
, selector_id
);
129 "usb-audio:%d: setting selector (id %d) to %x failed (current: %d)\n",
130 chip
->dev
->devnum
, selector_id
, pin
, ret
);
137 static bool uac_clock_source_is_valid(struct snd_usb_audio
*chip
, int source_id
)
141 struct usb_device
*dev
= chip
->dev
;
142 struct uac_clock_source_descriptor
*cs_desc
=
143 snd_usb_find_clock_source(chip
->ctrl_intf
, source_id
);
148 /* If a clock source can't tell us whether it's valid, we assume it is */
149 if (!uac2_control_is_readable(cs_desc
->bmControls
,
150 UAC2_CS_CONTROL_CLOCK_VALID
- 1))
153 err
= snd_usb_ctl_msg(dev
, usb_rcvctrlpipe(dev
, 0), UAC2_CS_CUR
,
154 USB_TYPE_CLASS
| USB_RECIP_INTERFACE
| USB_DIR_IN
,
155 UAC2_CS_CONTROL_CLOCK_VALID
<< 8,
156 snd_usb_ctrl_intf(chip
) | (source_id
<< 8),
157 &data
, sizeof(data
));
160 snd_printk(KERN_WARNING
"%s(): cannot get clock validity for id %d\n",
161 __func__
, source_id
);
168 static int __uac_clock_find_source(struct snd_usb_audio
*chip
,
169 int entity_id
, unsigned long *visited
,
172 struct uac_clock_source_descriptor
*source
;
173 struct uac_clock_selector_descriptor
*selector
;
174 struct uac_clock_multiplier_descriptor
*multiplier
;
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
);
185 /* first, see if the ID we're looking for is a clock source already */
186 source
= snd_usb_find_clock_source(chip
->ctrl_intf
, entity_id
);
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
);
197 selector
= snd_usb_find_clock_selector(chip
->ctrl_intf
, entity_id
);
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
);
207 /* Selector values are one-based */
209 if (ret
> selector
->bNrInPins
|| ret
< 1) {
211 "%s(): selector reported illegal value, id %d, ret %d\n",
212 __func__
, selector
->bClockID
, ret
);
218 ret
= __uac_clock_find_source(chip
, selector
->baCSourceID
[ret
- 1],
220 if (!validate
|| ret
> 0 || !chip
->autoclock
)
223 /* The current clock source is invalid, try others. */
224 for (i
= 1; i
<= selector
->bNrInPins
; i
++) {
230 ret
= __uac_clock_find_source(chip
, selector
->baCSourceID
[i
- 1],
235 err
= uac_clock_selector_set_val(chip
, entity_id
, i
);
240 "usb-audio:%d: found and selected valid clock source %d\n",
241 chip
->dev
->devnum
, ret
);
248 /* FIXME: multipliers only act as pass-thru element for now */
249 multiplier
= snd_usb_find_clock_multiplier(chip
->ctrl_intf
, entity_id
);
251 return __uac_clock_find_source(chip
, multiplier
->bCSourceID
,
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.
264 * The 'visited' bitfield is used internally to detect recursive loops.
266 * Returns the clock source UnitID (>=0) on success, or an error.
268 int snd_usb_clock_find_source(struct snd_usb_audio
*chip
, int entity_id
,
271 DECLARE_BITMAP(visited
, 256);
272 memset(visited
, 0, sizeof(visited
));
273 return __uac_clock_find_source(chip
, entity_id
, visited
, validate
);
276 static int set_sample_rate_v1(struct snd_usb_audio
*chip
, int iface
,
277 struct usb_host_interface
*alts
,
278 struct audioformat
*fmt
, int rate
)
280 struct usb_device
*dev
= chip
->dev
;
282 unsigned char data
[3];
285 ep
= get_endpoint(alts
, 0)->bEndpointAddress
;
287 /* if endpoint doesn't have sampling rate control, bail out */
288 if (!(fmt
->attributes
& UAC_EP_CS_ATTR_SAMPLE_RATE
))
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
,
297 data
, sizeof(data
))) < 0) {
298 snd_printk(KERN_ERR
"%d:%d:%d: cannot set freq %d to ep %#x\n",
299 dev
->devnum
, iface
, fmt
->altsetting
, rate
, ep
);
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
,
306 data
, sizeof(data
))) < 0) {
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 */
312 crate
= data
[0] | (data
[1] << 8) | (data
[2] << 16);
314 snd_printd(KERN_WARNING
"current rate %d is different from the runtime rate %d\n", crate
, rate
);
315 // runtime->rate = crate;
321 static int get_sample_rate_v2(struct snd_usb_audio
*chip
, int iface
,
322 int altsetting
, int clock
)
324 struct usb_device
*dev
= chip
->dev
;
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),
332 &data
, sizeof(data
));
334 snd_printk(KERN_WARNING
"%d:%d:%d: cannot get freq (v2)\n",
335 dev
->devnum
, iface
, altsetting
);
339 return le32_to_cpu(data
);
342 static int set_sample_rate_v2(struct snd_usb_audio
*chip
, int iface
,
343 struct usb_host_interface
*alts
,
344 struct audioformat
*fmt
, int rate
)
346 struct usb_device
*dev
= chip
->dev
;
348 int err
, cur_rate
, prev_rate
;
351 clock
= snd_usb_clock_find_source(chip
, fmt
->clock
, true);
355 prev_rate
= get_sample_rate_v2(chip
, iface
, fmt
->altsetting
, clock
);
357 data
= cpu_to_le32(rate
);
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
,
360 UAC2_CS_CONTROL_SAM_FREQ
<< 8,
361 snd_usb_ctrl_intf(chip
) | (clock
<< 8),
362 &data
, sizeof(data
))) < 0) {
363 snd_printk(KERN_ERR
"%d:%d:%d: cannot set freq %d (v2)\n",
364 dev
->devnum
, iface
, fmt
->altsetting
, rate
);
368 cur_rate
= get_sample_rate_v2(chip
, iface
, fmt
->altsetting
, clock
);
370 if (cur_rate
!= rate
) {
371 snd_printd(KERN_WARNING
372 "current rate %d is different from the runtime rate %d\n",
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
);
386 int snd_usb_init_sample_rate(struct snd_usb_audio
*chip
, int iface
,
387 struct usb_host_interface
*alts
,
388 struct audioformat
*fmt
, int rate
)
390 struct usb_interface_descriptor
*altsd
= get_iface_desc(alts
);
392 switch (altsd
->bInterfaceProtocol
) {
395 return set_sample_rate_v1(chip
, iface
, alts
, fmt
, rate
);
398 return set_sample_rate_v2(chip
, iface
, alts
, fmt
, rate
);