blob: 25de7fe285d9f8e34e58e88b767d11d149ba4ece [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>
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +020026#include <linux/usb/audio-v3.h>
Daniel Mack79f920f2010-05-31 14:51:31 +020027
28#include <sound/core.h>
29#include <sound/info.h>
30#include <sound/pcm.h>
Daniel Mack79f920f2010-05-31 14:51:31 +020031
32#include "usbaudio.h"
33#include "card.h"
Daniel Mack79f920f2010-05-31 14:51:31 +020034#include "helper.h"
Daniel Mackf22aa942010-06-16 17:57:27 +020035#include "clock.h"
Daniel Mack21bb5aa2013-04-10 00:56:03 +080036#include "quirks.h"
Daniel Mack79f920f2010-05-31 14:51:31 +020037
38static struct uac_clock_source_descriptor *
39 snd_usb_find_clock_source(struct usb_host_interface *ctrl_iface,
40 int clock_id)
41{
42 struct uac_clock_source_descriptor *cs = NULL;
43
44 while ((cs = snd_usb_find_csint_desc(ctrl_iface->extra,
45 ctrl_iface->extralen,
46 cs, UAC2_CLOCK_SOURCE))) {
Takashi Iwai0a62d6c2017-11-21 17:28:06 +010047 if (cs->bLength >= sizeof(*cs) && cs->bClockID == clock_id)
Daniel Mack79f920f2010-05-31 14:51:31 +020048 return cs;
49 }
50
51 return NULL;
52}
53
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +020054static struct uac3_clock_source_descriptor *
55 snd_usb_find_clock_source_v3(struct usb_host_interface *ctrl_iface,
56 int clock_id)
57{
58 struct uac3_clock_source_descriptor *cs = NULL;
59
60 while ((cs = snd_usb_find_csint_desc(ctrl_iface->extra,
61 ctrl_iface->extralen,
62 cs, UAC3_CLOCK_SOURCE))) {
63 if (cs->bClockID == clock_id)
64 return cs;
65 }
66
67 return NULL;
68}
69
Daniel Mack79f920f2010-05-31 14:51:31 +020070static struct uac_clock_selector_descriptor *
71 snd_usb_find_clock_selector(struct usb_host_interface *ctrl_iface,
72 int clock_id)
73{
74 struct uac_clock_selector_descriptor *cs = NULL;
75
76 while ((cs = snd_usb_find_csint_desc(ctrl_iface->extra,
77 ctrl_iface->extralen,
78 cs, UAC2_CLOCK_SELECTOR))) {
Takashi Iwai0a62d6c2017-11-21 17:28:06 +010079 if (cs->bLength >= sizeof(*cs) && cs->bClockID == clock_id) {
80 if (cs->bLength < 5 + cs->bNrInPins)
81 return NULL;
Daniel Mack79f920f2010-05-31 14:51:31 +020082 return cs;
Takashi Iwai0a62d6c2017-11-21 17:28:06 +010083 }
Daniel Mack79f920f2010-05-31 14:51:31 +020084 }
85
86 return NULL;
87}
88
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +020089static struct uac3_clock_selector_descriptor *
90 snd_usb_find_clock_selector_v3(struct usb_host_interface *ctrl_iface,
91 int clock_id)
92{
93 struct uac3_clock_selector_descriptor *cs = NULL;
94
95 while ((cs = snd_usb_find_csint_desc(ctrl_iface->extra,
96 ctrl_iface->extralen,
97 cs, UAC3_CLOCK_SELECTOR))) {
98 if (cs->bClockID == clock_id)
99 return cs;
100 }
101
102 return NULL;
103}
104
Daniel Mack79f920f2010-05-31 14:51:31 +0200105static struct uac_clock_multiplier_descriptor *
106 snd_usb_find_clock_multiplier(struct usb_host_interface *ctrl_iface,
107 int clock_id)
108{
109 struct uac_clock_multiplier_descriptor *cs = NULL;
110
111 while ((cs = snd_usb_find_csint_desc(ctrl_iface->extra,
112 ctrl_iface->extralen,
113 cs, UAC2_CLOCK_MULTIPLIER))) {
Takashi Iwai0a62d6c2017-11-21 17:28:06 +0100114 if (cs->bLength >= sizeof(*cs) && cs->bClockID == clock_id)
Daniel Mack79f920f2010-05-31 14:51:31 +0200115 return cs;
116 }
117
118 return NULL;
119}
120
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +0200121static struct uac3_clock_multiplier_descriptor *
122 snd_usb_find_clock_multiplier_v3(struct usb_host_interface *ctrl_iface,
123 int clock_id)
124{
125 struct uac3_clock_multiplier_descriptor *cs = NULL;
126
127 while ((cs = snd_usb_find_csint_desc(ctrl_iface->extra,
128 ctrl_iface->extralen,
129 cs, UAC3_CLOCK_MULTIPLIER))) {
130 if (cs->bClockID == clock_id)
131 return cs;
132 }
133
134 return NULL;
135}
136
Daniel Mack79f920f2010-05-31 14:51:31 +0200137static int uac_clock_selector_get_val(struct snd_usb_audio *chip, int selector_id)
138{
139 unsigned char buf;
140 int ret;
141
142 ret = snd_usb_ctl_msg(chip->dev, usb_rcvctrlpipe(chip->dev, 0),
143 UAC2_CS_CUR,
144 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
Daniel Mack11bcbc42010-06-11 17:34:20 +0200145 UAC2_CX_CLOCK_SELECTOR << 8,
146 snd_usb_ctrl_intf(chip) | (selector_id << 8),
Clemens Ladisch17d900c2011-09-26 21:15:27 +0200147 &buf, sizeof(buf));
Daniel Mack79f920f2010-05-31 14:51:31 +0200148
149 if (ret < 0)
150 return ret;
151
152 return buf;
153}
154
Eldad Zack8c55af32013-04-03 23:18:55 +0200155static int uac_clock_selector_set_val(struct snd_usb_audio *chip, int selector_id,
156 unsigned char pin)
157{
158 int ret;
159
160 ret = snd_usb_ctl_msg(chip->dev, usb_sndctrlpipe(chip->dev, 0),
161 UAC2_CS_CUR,
162 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
163 UAC2_CX_CLOCK_SELECTOR << 8,
164 snd_usb_ctrl_intf(chip) | (selector_id << 8),
165 &pin, sizeof(pin));
166 if (ret < 0)
167 return ret;
168
169 if (ret != sizeof(pin)) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100170 usb_audio_err(chip,
171 "setting selector (id %d) unexpected length %d\n",
172 selector_id, ret);
Eldad Zack8c55af32013-04-03 23:18:55 +0200173 return -EINVAL;
174 }
175
176 ret = uac_clock_selector_get_val(chip, selector_id);
177 if (ret < 0)
178 return ret;
179
180 if (ret != pin) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100181 usb_audio_err(chip,
182 "setting selector (id %d) to %x failed (current: %d)\n",
183 selector_id, pin, ret);
Eldad Zack8c55af32013-04-03 23:18:55 +0200184 return -EINVAL;
185 }
186
187 return ret;
188}
189
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +0200190static bool uac_clock_source_is_valid(struct snd_usb_audio *chip,
191 int protocol,
192 int source_id)
Daniel Mack79f920f2010-05-31 14:51:31 +0200193{
194 int err;
195 unsigned char data;
196 struct usb_device *dev = chip->dev;
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +0200197 u32 bmControls;
Daniel Mack3bc6fbc72011-05-18 11:28:44 +0200198
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +0200199 if (protocol == UAC_VERSION_3) {
200 struct uac3_clock_source_descriptor *cs_desc =
201 snd_usb_find_clock_source_v3(chip->ctrl_intf, source_id);
202
203 if (!cs_desc)
204 return 0;
205 bmControls = le32_to_cpu(cs_desc->bmControls);
206 } else { /* UAC_VERSION_1/2 */
207 struct uac_clock_source_descriptor *cs_desc =
208 snd_usb_find_clock_source(chip->ctrl_intf, source_id);
209
210 if (!cs_desc)
211 return 0;
212 bmControls = cs_desc->bmControls;
213 }
Daniel Mack3bc6fbc72011-05-18 11:28:44 +0200214
215 /* If a clock source can't tell us whether it's valid, we assume it is */
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +0200216 if (!uac_v2v3_control_is_readable(bmControls,
Daniel Mackaff252a2012-08-01 10:16:53 +0200217 UAC2_CS_CONTROL_CLOCK_VALID - 1))
Daniel Mack3bc6fbc72011-05-18 11:28:44 +0200218 return 1;
Daniel Mack79f920f2010-05-31 14:51:31 +0200219
220 err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_CUR,
221 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
Daniel Mack11bcbc42010-06-11 17:34:20 +0200222 UAC2_CS_CONTROL_CLOCK_VALID << 8,
223 snd_usb_ctrl_intf(chip) | (source_id << 8),
Clemens Ladisch17d900c2011-09-26 21:15:27 +0200224 &data, sizeof(data));
Daniel Mack79f920f2010-05-31 14:51:31 +0200225
226 if (err < 0) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100227 dev_warn(&dev->dev,
228 "%s(): cannot get clock validity for id %d\n",
Daniel Mack79f920f2010-05-31 14:51:31 +0200229 __func__, source_id);
Daniel Mack3bc6fbc72011-05-18 11:28:44 +0200230 return 0;
Daniel Mack79f920f2010-05-31 14:51:31 +0200231 }
232
233 return !!data;
234}
235
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +0200236static int __uac_clock_find_source(struct snd_usb_audio *chip, int entity_id,
237 unsigned long *visited, bool validate)
Daniel Mack79f920f2010-05-31 14:51:31 +0200238{
239 struct uac_clock_source_descriptor *source;
240 struct uac_clock_selector_descriptor *selector;
241 struct uac_clock_multiplier_descriptor *multiplier;
242
243 entity_id &= 0xff;
244
245 if (test_and_set_bit(entity_id, visited)) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100246 usb_audio_warn(chip,
247 "%s(): recursive clock topology detected, id %d.\n",
248 __func__, entity_id);
Daniel Mack79f920f2010-05-31 14:51:31 +0200249 return -EINVAL;
250 }
251
252 /* first, see if the ID we're looking for is a clock source already */
Daniel Mack3d8d4dc2010-06-16 17:57:31 +0200253 source = snd_usb_find_clock_source(chip->ctrl_intf, entity_id);
Eldad Zack06ffc1e2013-04-03 23:18:54 +0200254 if (source) {
255 entity_id = source->bClockID;
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +0200256 if (validate && !uac_clock_source_is_valid(chip, UAC_VERSION_2,
257 entity_id)) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100258 usb_audio_err(chip,
259 "clock source %d is not valid, cannot use\n",
260 entity_id);
Eldad Zack06ffc1e2013-04-03 23:18:54 +0200261 return -ENXIO;
262 }
263 return entity_id;
264 }
Daniel Mack79f920f2010-05-31 14:51:31 +0200265
Daniel Mack3d8d4dc2010-06-16 17:57:31 +0200266 selector = snd_usb_find_clock_selector(chip->ctrl_intf, entity_id);
Daniel Mack79f920f2010-05-31 14:51:31 +0200267 if (selector) {
Eldad Zack8c55af32013-04-03 23:18:55 +0200268 int ret, i, cur;
Daniel Mack79f920f2010-05-31 14:51:31 +0200269
270 /* the entity ID we are looking for is a selector.
271 * find out what it currently selects */
272 ret = uac_clock_selector_get_val(chip, selector->bClockID);
273 if (ret < 0)
274 return ret;
275
Daniel Mack157a57b2010-06-16 17:57:30 +0200276 /* Selector values are one-based */
277
Daniel Mack79f920f2010-05-31 14:51:31 +0200278 if (ret > selector->bNrInPins || ret < 1) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100279 usb_audio_err(chip,
Daniel Mack79f920f2010-05-31 14:51:31 +0200280 "%s(): selector reported illegal value, id %d, ret %d\n",
281 __func__, selector->bClockID, ret);
282
283 return -EINVAL;
284 }
285
Eldad Zack8c55af32013-04-03 23:18:55 +0200286 cur = ret;
287 ret = __uac_clock_find_source(chip, selector->baCSourceID[ret - 1],
Eldad Zack06ffc1e2013-04-03 23:18:54 +0200288 visited, validate);
Eldad Zackef02e292013-04-03 23:18:56 +0200289 if (!validate || ret > 0 || !chip->autoclock)
Eldad Zack8c55af32013-04-03 23:18:55 +0200290 return ret;
291
292 /* The current clock source is invalid, try others. */
293 for (i = 1; i <= selector->bNrInPins; i++) {
294 int err;
295
296 if (i == cur)
297 continue;
298
299 ret = __uac_clock_find_source(chip, selector->baCSourceID[i - 1],
300 visited, true);
301 if (ret < 0)
302 continue;
303
304 err = uac_clock_selector_set_val(chip, entity_id, i);
305 if (err < 0)
306 continue;
307
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100308 usb_audio_info(chip,
309 "found and selected valid clock source %d\n",
310 ret);
Eldad Zack8c55af32013-04-03 23:18:55 +0200311 return ret;
312 }
313
314 return -ENXIO;
Daniel Mack79f920f2010-05-31 14:51:31 +0200315 }
316
317 /* FIXME: multipliers only act as pass-thru element for now */
Daniel Mack3d8d4dc2010-06-16 17:57:31 +0200318 multiplier = snd_usb_find_clock_multiplier(chip->ctrl_intf, entity_id);
Daniel Mack79f920f2010-05-31 14:51:31 +0200319 if (multiplier)
Daniel Mack3d8d4dc2010-06-16 17:57:31 +0200320 return __uac_clock_find_source(chip, multiplier->bCSourceID,
Eldad Zack06ffc1e2013-04-03 23:18:54 +0200321 visited, validate);
Daniel Mack79f920f2010-05-31 14:51:31 +0200322
323 return -EINVAL;
324}
325
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +0200326static int __uac3_clock_find_source(struct snd_usb_audio *chip, int entity_id,
327 unsigned long *visited, bool validate)
328{
329 struct uac3_clock_source_descriptor *source;
330 struct uac3_clock_selector_descriptor *selector;
331 struct uac3_clock_multiplier_descriptor *multiplier;
332
333 entity_id &= 0xff;
334
335 if (test_and_set_bit(entity_id, visited)) {
336 usb_audio_warn(chip,
337 "%s(): recursive clock topology detected, id %d.\n",
338 __func__, entity_id);
339 return -EINVAL;
340 }
341
342 /* first, see if the ID we're looking for is a clock source already */
343 source = snd_usb_find_clock_source_v3(chip->ctrl_intf, entity_id);
344 if (source) {
345 entity_id = source->bClockID;
346 if (validate && !uac_clock_source_is_valid(chip, UAC_VERSION_3,
347 entity_id)) {
348 usb_audio_err(chip,
349 "clock source %d is not valid, cannot use\n",
350 entity_id);
351 return -ENXIO;
352 }
353 return entity_id;
354 }
355
356 selector = snd_usb_find_clock_selector_v3(chip->ctrl_intf, entity_id);
357 if (selector) {
358 int ret, i, cur;
359
360 /* the entity ID we are looking for is a selector.
361 * find out what it currently selects */
362 ret = uac_clock_selector_get_val(chip, selector->bClockID);
363 if (ret < 0)
364 return ret;
365
366 /* Selector values are one-based */
367
368 if (ret > selector->bNrInPins || ret < 1) {
369 usb_audio_err(chip,
370 "%s(): selector reported illegal value, id %d, ret %d\n",
371 __func__, selector->bClockID, ret);
372
373 return -EINVAL;
374 }
375
376 cur = ret;
377 ret = __uac3_clock_find_source(chip, selector->baCSourceID[ret - 1],
378 visited, validate);
379 if (!validate || ret > 0 || !chip->autoclock)
380 return ret;
381
382 /* The current clock source is invalid, try others. */
383 for (i = 1; i <= selector->bNrInPins; i++) {
384 int err;
385
386 if (i == cur)
387 continue;
388
389 ret = __uac3_clock_find_source(chip, selector->baCSourceID[i - 1],
390 visited, true);
391 if (ret < 0)
392 continue;
393
394 err = uac_clock_selector_set_val(chip, entity_id, i);
395 if (err < 0)
396 continue;
397
398 usb_audio_info(chip,
399 "found and selected valid clock source %d\n",
400 ret);
401 return ret;
402 }
403
404 return -ENXIO;
405 }
406
407 /* FIXME: multipliers only act as pass-thru element for now */
408 multiplier = snd_usb_find_clock_multiplier_v3(chip->ctrl_intf,
409 entity_id);
410 if (multiplier)
411 return __uac3_clock_find_source(chip, multiplier->bCSourceID,
412 visited, validate);
413
414 return -EINVAL;
415}
416
Daniel Mack157a57b2010-06-16 17:57:30 +0200417/*
418 * For all kinds of sample rate settings and other device queries,
419 * the clock source (end-leaf) must be used. However, clock selectors,
420 * clock multipliers and sample rate converters may be specified as
421 * clock source input to terminal. This functions walks the clock path
422 * to its end and tries to find the source.
423 *
424 * The 'visited' bitfield is used internally to detect recursive loops.
425 *
426 * Returns the clock source UnitID (>=0) on success, or an error.
427 */
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +0200428int snd_usb_clock_find_source(struct snd_usb_audio *chip, int protocol,
429 int entity_id, bool validate)
Daniel Mack79f920f2010-05-31 14:51:31 +0200430{
431 DECLARE_BITMAP(visited, 256);
432 memset(visited, 0, sizeof(visited));
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +0200433
434 switch (protocol) {
435 case UAC_VERSION_2:
436 return __uac_clock_find_source(chip, entity_id, visited,
437 validate);
438 case UAC_VERSION_3:
439 return __uac3_clock_find_source(chip, entity_id, visited,
440 validate);
441 default:
442 return -EINVAL;
443 }
Daniel Mack79f920f2010-05-31 14:51:31 +0200444}
445
446static int set_sample_rate_v1(struct snd_usb_audio *chip, int iface,
447 struct usb_host_interface *alts,
448 struct audioformat *fmt, int rate)
449{
450 struct usb_device *dev = chip->dev;
451 unsigned int ep;
452 unsigned char data[3];
453 int err, crate;
454
Takashi Iwai447d6272016-03-15 15:20:58 +0100455 if (get_iface_desc(alts)->bNumEndpoints < 1)
456 return -EINVAL;
Daniel Mack79f920f2010-05-31 14:51:31 +0200457 ep = get_endpoint(alts, 0)->bEndpointAddress;
458
459 /* if endpoint doesn't have sampling rate control, bail out */
Clemens Ladischd32d5522010-07-08 16:38:01 +0200460 if (!(fmt->attributes & UAC_EP_CS_ATTR_SAMPLE_RATE))
Daniel Mack79f920f2010-05-31 14:51:31 +0200461 return 0;
Daniel Mack79f920f2010-05-31 14:51:31 +0200462
463 data[0] = rate;
464 data[1] = rate >> 8;
465 data[2] = rate >> 16;
466 if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR,
467 USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_OUT,
468 UAC_EP_CS_ATTR_SAMPLE_RATE << 8, ep,
Clemens Ladisch17d900c2011-09-26 21:15:27 +0200469 data, sizeof(data))) < 0) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100470 dev_err(&dev->dev, "%d:%d: cannot set freq %d to ep %#x\n",
471 iface, fmt->altsetting, rate, ep);
Daniel Mack79f920f2010-05-31 14:51:31 +0200472 return err;
473 }
474
Joe Turnerb62b9982015-02-16 20:44:33 +0000475 /* Don't check the sample rate for devices which we know don't
476 * support reading */
477 if (snd_usb_get_sample_rate_quirk(chip))
478 return 0;
Takashi Iwai57dd5412016-04-29 11:49:04 +0200479 /* the firmware is likely buggy, don't repeat to fail too many times */
480 if (chip->sample_rate_read_error > 2)
481 return 0;
Joe Turnerb62b9982015-02-16 20:44:33 +0000482
Daniel Mack79f920f2010-05-31 14:51:31 +0200483 if ((err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC_GET_CUR,
484 USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_IN,
485 UAC_EP_CS_ATTR_SAMPLE_RATE << 8, ep,
Clemens Ladisch17d900c2011-09-26 21:15:27 +0200486 data, sizeof(data))) < 0) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100487 dev_err(&dev->dev, "%d:%d: cannot get freq at ep %#x\n",
488 iface, fmt->altsetting, ep);
Takashi Iwai57dd5412016-04-29 11:49:04 +0200489 chip->sample_rate_read_error++;
Daniel Mack79f920f2010-05-31 14:51:31 +0200490 return 0; /* some devices don't support reading */
491 }
492
493 crate = data[0] | (data[1] << 8) | (data[2] << 16);
494 if (crate != rate) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100495 dev_warn(&dev->dev, "current rate %d is different from the runtime rate %d\n", crate, rate);
Daniel Mack79f920f2010-05-31 14:51:31 +0200496 // runtime->rate = crate;
497 }
498
499 return 0;
500}
501
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +0200502static int get_sample_rate_v2v3(struct snd_usb_audio *chip, int iface,
Takashi Iwai7c5174652013-04-03 19:08:29 +0200503 int altsetting, int clock)
504{
505 struct usb_device *dev = chip->dev;
Eldad Zackf6a8bc72013-04-03 23:18:53 +0200506 __le32 data;
Takashi Iwai7c5174652013-04-03 19:08:29 +0200507 int err;
508
509 err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_CUR,
510 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
511 UAC2_CS_CONTROL_SAM_FREQ << 8,
512 snd_usb_ctrl_intf(chip) | (clock << 8),
Eldad Zackf6a8bc72013-04-03 23:18:53 +0200513 &data, sizeof(data));
Takashi Iwai7c5174652013-04-03 19:08:29 +0200514 if (err < 0) {
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +0200515 dev_warn(&dev->dev, "%d:%d: cannot get freq (v2/v3): err %d\n",
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100516 iface, altsetting, err);
Takashi Iwai7c5174652013-04-03 19:08:29 +0200517 return 0;
518 }
519
Eldad Zackf6a8bc72013-04-03 23:18:53 +0200520 return le32_to_cpu(data);
Takashi Iwai7c5174652013-04-03 19:08:29 +0200521}
522
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +0200523static int set_sample_rate_v2v3(struct snd_usb_audio *chip, int iface,
Daniel Mack79f920f2010-05-31 14:51:31 +0200524 struct usb_host_interface *alts,
525 struct audioformat *fmt, int rate)
526{
527 struct usb_device *dev = chip->dev;
Eldad Zackf6a8bc72013-04-03 23:18:53 +0200528 __le32 data;
Torstein Hegge690a8632013-03-26 22:10:05 +0100529 int err, cur_rate, prev_rate;
Eldad Zack8c55af32013-04-03 23:18:55 +0200530 int clock;
Eldad Zack1dc669f2013-04-03 23:18:58 +0200531 bool writeable;
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +0200532 u32 bmControls;
Daniel Mack79f920f2010-05-31 14:51:31 +0200533
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +0200534 clock = snd_usb_clock_find_source(chip, fmt->protocol,
535 fmt->clock, true);
Daniel Mack79f920f2010-05-31 14:51:31 +0200536 if (clock < 0)
537 return clock;
538
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +0200539 prev_rate = get_sample_rate_v2v3(chip, iface, fmt->altsetting, clock);
David Henningssonfa92dd72013-04-25 14:32:24 +0200540 if (prev_rate == rate)
541 return 0;
Torstein Hegge690a8632013-03-26 22:10:05 +0100542
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +0200543 if (fmt->protocol == UAC_VERSION_3) {
544 struct uac3_clock_source_descriptor *cs_desc;
545
546 cs_desc = snd_usb_find_clock_source_v3(chip->ctrl_intf, clock);
547 bmControls = le32_to_cpu(cs_desc->bmControls);
548 } else {
549 struct uac_clock_source_descriptor *cs_desc;
550
551 cs_desc = snd_usb_find_clock_source(chip->ctrl_intf, clock);
552 bmControls = cs_desc->bmControls;
553 }
554
555 writeable = uac_v2v3_control_is_writeable(bmControls, UAC2_CS_CONTROL_SAM_FREQ - 1);
Eldad Zack1dc669f2013-04-03 23:18:58 +0200556 if (writeable) {
557 data = cpu_to_le32(rate);
558 err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC2_CS_CUR,
559 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
560 UAC2_CS_CONTROL_SAM_FREQ << 8,
561 snd_usb_ctrl_intf(chip) | (clock << 8),
562 &data, sizeof(data));
563 if (err < 0) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100564 usb_audio_err(chip,
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +0200565 "%d:%d: cannot set freq %d (v2/v3): err %d\n",
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100566 iface, fmt->altsetting, rate, err);
Eldad Zack1dc669f2013-04-03 23:18:58 +0200567 return err;
568 }
569
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +0200570 cur_rate = get_sample_rate_v2v3(chip, iface,
571 fmt->altsetting, clock);
Eldad Zack1dc669f2013-04-03 23:18:58 +0200572 } else {
573 cur_rate = prev_rate;
Daniel Mack79f920f2010-05-31 14:51:31 +0200574 }
575
Torstein Hegge690a8632013-03-26 22:10:05 +0100576 if (cur_rate != rate) {
Eldad Zack1dc669f2013-04-03 23:18:58 +0200577 if (!writeable) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100578 usb_audio_warn(chip,
579 "%d:%d: freq mismatch (RO clock): req %d, clock runs @%d\n",
580 iface, fmt->altsetting, rate, cur_rate);
Eldad Zack1dc669f2013-04-03 23:18:58 +0200581 return -ENXIO;
582 }
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100583 usb_audio_dbg(chip,
584 "current rate %d is different from the runtime rate %d\n",
585 cur_rate, rate);
Torstein Hegge690a8632013-03-26 22:10:05 +0100586 }
587
588 /* Some devices doesn't respond to sample rate changes while the
589 * interface is active. */
590 if (rate != prev_rate) {
591 usb_set_interface(dev, iface, 0);
Daniel Mack21bb5aa2013-04-10 00:56:03 +0800592 snd_usb_set_interface_quirk(dev);
Torstein Hegge690a8632013-03-26 22:10:05 +0100593 usb_set_interface(dev, iface, fmt->altsetting);
Daniel Mack21bb5aa2013-04-10 00:56:03 +0800594 snd_usb_set_interface_quirk(dev);
Torstein Hegge690a8632013-03-26 22:10:05 +0100595 }
Daniel Mack79f920f2010-05-31 14:51:31 +0200596
597 return 0;
598}
599
600int snd_usb_init_sample_rate(struct snd_usb_audio *chip, int iface,
601 struct usb_host_interface *alts,
602 struct audioformat *fmt, int rate)
603{
Clemens Ladisch8f898e92013-01-31 21:39:17 +0100604 switch (fmt->protocol) {
Daniel Mack79f920f2010-05-31 14:51:31 +0200605 case UAC_VERSION_1:
Clemens Ladischa2acad82010-09-03 10:53:11 +0200606 default:
Daniel Mack79f920f2010-05-31 14:51:31 +0200607 return set_sample_rate_v1(chip, iface, alts, fmt, rate);
608
609 case UAC_VERSION_2:
Ruslan Bilovol9a2fe9b2018-03-21 02:03:59 +0200610 case UAC_VERSION_3:
611 return set_sample_rate_v2v3(chip, iface, alts, fmt, rate);
Daniel Mack79f920f2010-05-31 14:51:31 +0200612 }
Daniel Mack79f920f2010-05-31 14:51:31 +0200613}
614