xref: /openbmc/linux/sound/usb/clock.c (revision b240b419db5d624ce7a5a397d6f62a1a686009ec)
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>
22 #include <linux/string.h>
23 #include <linux/usb.h>
24 #include <linux/usb/audio.h>
25 #include <linux/usb/audio-v2.h>
26 #include <linux/usb/audio-v3.h>
27 
28 #include <sound/core.h>
29 #include <sound/info.h>
30 #include <sound/pcm.h>
31 
32 #include "usbaudio.h"
33 #include "card.h"
34 #include "helper.h"
35 #include "clock.h"
36 #include "quirks.h"
37 
38 static 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))) {
47 		if (cs->bLength >= sizeof(*cs) && cs->bClockID == clock_id)
48 			return cs;
49 	}
50 
51 	return NULL;
52 }
53 
54 static 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 
70 static 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))) {
79 		if (cs->bLength >= sizeof(*cs) && cs->bClockID == clock_id) {
80 			if (cs->bLength < 5 + cs->bNrInPins)
81 				return NULL;
82 			return cs;
83 		}
84 	}
85 
86 	return NULL;
87 }
88 
89 static 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 
105 static 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))) {
114 		if (cs->bLength >= sizeof(*cs) && cs->bClockID == clock_id)
115 			return cs;
116 	}
117 
118 	return NULL;
119 }
120 
121 static 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 
137 static 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,
145 			      UAC2_CX_CLOCK_SELECTOR << 8,
146 			      snd_usb_ctrl_intf(chip) | (selector_id << 8),
147 			      &buf, sizeof(buf));
148 
149 	if (ret < 0)
150 		return ret;
151 
152 	return buf;
153 }
154 
155 static 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)) {
170 		usb_audio_err(chip,
171 			"setting selector (id %d) unexpected length %d\n",
172 			selector_id, ret);
173 		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) {
181 		usb_audio_err(chip,
182 			"setting selector (id %d) to %x failed (current: %d)\n",
183 			selector_id, pin, ret);
184 		return -EINVAL;
185 	}
186 
187 	return ret;
188 }
189 
190 static bool uac_clock_source_is_valid(struct snd_usb_audio *chip,
191 				      int protocol,
192 				      int source_id)
193 {
194 	int err;
195 	unsigned char data;
196 	struct usb_device *dev = chip->dev;
197 	u32 bmControls;
198 
199 	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 	}
214 
215 	/* If a clock source can't tell us whether it's valid, we assume it is */
216 	if (!uac_v2v3_control_is_readable(bmControls,
217 				      UAC2_CS_CONTROL_CLOCK_VALID))
218 		return 1;
219 
220 	err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_CUR,
221 			      USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
222 			      UAC2_CS_CONTROL_CLOCK_VALID << 8,
223 			      snd_usb_ctrl_intf(chip) | (source_id << 8),
224 			      &data, sizeof(data));
225 
226 	if (err < 0) {
227 		dev_warn(&dev->dev,
228 			 "%s(): cannot get clock validity for id %d\n",
229 			   __func__, source_id);
230 		return 0;
231 	}
232 
233 	return !!data;
234 }
235 
236 static int __uac_clock_find_source(struct snd_usb_audio *chip, int entity_id,
237 				   unsigned long *visited, bool validate)
238 {
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)) {
246 		usb_audio_warn(chip,
247 			 "%s(): recursive clock topology detected, id %d.\n",
248 			 __func__, entity_id);
249 		return -EINVAL;
250 	}
251 
252 	/* first, see if the ID we're looking for is a clock source already */
253 	source = snd_usb_find_clock_source(chip->ctrl_intf, entity_id);
254 	if (source) {
255 		entity_id = source->bClockID;
256 		if (validate && !uac_clock_source_is_valid(chip, UAC_VERSION_2,
257 								entity_id)) {
258 			usb_audio_err(chip,
259 				"clock source %d is not valid, cannot use\n",
260 				entity_id);
261 			return -ENXIO;
262 		}
263 		return entity_id;
264 	}
265 
266 	selector = snd_usb_find_clock_selector(chip->ctrl_intf, entity_id);
267 	if (selector) {
268 		int ret, i, cur;
269 
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 
276 		/* Selector values are one-based */
277 
278 		if (ret > selector->bNrInPins || ret < 1) {
279 			usb_audio_err(chip,
280 				"%s(): selector reported illegal value, id %d, ret %d\n",
281 				__func__, selector->bClockID, ret);
282 
283 			return -EINVAL;
284 		}
285 
286 		cur = ret;
287 		ret = __uac_clock_find_source(chip, selector->baCSourceID[ret - 1],
288 					       visited, validate);
289 		if (!validate || ret > 0 || !chip->autoclock)
290 			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 
308 			usb_audio_info(chip,
309 				 "found and selected valid clock source %d\n",
310 				 ret);
311 			return ret;
312 		}
313 
314 		return -ENXIO;
315 	}
316 
317 	/* FIXME: multipliers only act as pass-thru element for now */
318 	multiplier = snd_usb_find_clock_multiplier(chip->ctrl_intf, entity_id);
319 	if (multiplier)
320 		return __uac_clock_find_source(chip, multiplier->bCSourceID,
321 						visited, validate);
322 
323 	return -EINVAL;
324 }
325 
326 static 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 
417 /*
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  */
428 int snd_usb_clock_find_source(struct snd_usb_audio *chip, int protocol,
429 			      int entity_id, bool validate)
430 {
431 	DECLARE_BITMAP(visited, 256);
432 	memset(visited, 0, sizeof(visited));
433 
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 	}
444 }
445 
446 static 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 
455 	if (get_iface_desc(alts)->bNumEndpoints < 1)
456 		return -EINVAL;
457 	ep = get_endpoint(alts, 0)->bEndpointAddress;
458 
459 	/* if endpoint doesn't have sampling rate control, bail out */
460 	if (!(fmt->attributes & UAC_EP_CS_ATTR_SAMPLE_RATE))
461 		return 0;
462 
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,
469 				   data, sizeof(data))) < 0) {
470 		dev_err(&dev->dev, "%d:%d: cannot set freq %d to ep %#x\n",
471 			iface, fmt->altsetting, rate, ep);
472 		return err;
473 	}
474 
475 	/* 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;
479 	/* the firmware is likely buggy, don't repeat to fail too many times */
480 	if (chip->sample_rate_read_error > 2)
481 		return 0;
482 
483 	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,
486 				   data, sizeof(data))) < 0) {
487 		dev_err(&dev->dev, "%d:%d: cannot get freq at ep %#x\n",
488 			iface, fmt->altsetting, ep);
489 		chip->sample_rate_read_error++;
490 		return 0; /* some devices don't support reading */
491 	}
492 
493 	crate = data[0] | (data[1] << 8) | (data[2] << 16);
494 	if (crate != rate) {
495 		dev_warn(&dev->dev, "current rate %d is different from the runtime rate %d\n", crate, rate);
496 		// runtime->rate = crate;
497 	}
498 
499 	return 0;
500 }
501 
502 static int get_sample_rate_v2v3(struct snd_usb_audio *chip, int iface,
503 			      int altsetting, int clock)
504 {
505 	struct usb_device *dev = chip->dev;
506 	__le32 data;
507 	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),
513 			      &data, sizeof(data));
514 	if (err < 0) {
515 		dev_warn(&dev->dev, "%d:%d: cannot get freq (v2/v3): err %d\n",
516 			 iface, altsetting, err);
517 		return 0;
518 	}
519 
520 	return le32_to_cpu(data);
521 }
522 
523 static int set_sample_rate_v2v3(struct snd_usb_audio *chip, int iface,
524 			      struct usb_host_interface *alts,
525 			      struct audioformat *fmt, int rate)
526 {
527 	struct usb_device *dev = chip->dev;
528 	__le32 data;
529 	int err, cur_rate, prev_rate;
530 	int clock;
531 	bool writeable;
532 	u32 bmControls;
533 
534 	clock = snd_usb_clock_find_source(chip, fmt->protocol,
535 					  fmt->clock, true);
536 	if (clock < 0)
537 		return clock;
538 
539 	prev_rate = get_sample_rate_v2v3(chip, iface, fmt->altsetting, clock);
540 	if (prev_rate == rate)
541 		return 0;
542 
543 	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,
556 						  UAC2_CS_CONTROL_SAM_FREQ);
557 	if (writeable) {
558 		data = cpu_to_le32(rate);
559 		err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC2_CS_CUR,
560 				      USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
561 				      UAC2_CS_CONTROL_SAM_FREQ << 8,
562 				      snd_usb_ctrl_intf(chip) | (clock << 8),
563 				      &data, sizeof(data));
564 		if (err < 0) {
565 			usb_audio_err(chip,
566 				"%d:%d: cannot set freq %d (v2/v3): err %d\n",
567 				iface, fmt->altsetting, rate, err);
568 			return err;
569 		}
570 
571 		cur_rate = get_sample_rate_v2v3(chip, iface,
572 						fmt->altsetting, clock);
573 	} else {
574 		cur_rate = prev_rate;
575 	}
576 
577 	if (cur_rate != rate) {
578 		if (!writeable) {
579 			usb_audio_warn(chip,
580 				 "%d:%d: freq mismatch (RO clock): req %d, clock runs @%d\n",
581 				 iface, fmt->altsetting, rate, cur_rate);
582 			return -ENXIO;
583 		}
584 		usb_audio_dbg(chip,
585 			"current rate %d is different from the runtime rate %d\n",
586 			cur_rate, rate);
587 	}
588 
589 	/* Some devices doesn't respond to sample rate changes while the
590 	 * interface is active. */
591 	if (rate != prev_rate) {
592 		usb_set_interface(dev, iface, 0);
593 		snd_usb_set_interface_quirk(dev);
594 		usb_set_interface(dev, iface, fmt->altsetting);
595 		snd_usb_set_interface_quirk(dev);
596 	}
597 
598 	return 0;
599 }
600 
601 int snd_usb_init_sample_rate(struct snd_usb_audio *chip, int iface,
602 			     struct usb_host_interface *alts,
603 			     struct audioformat *fmt, int rate)
604 {
605 	switch (fmt->protocol) {
606 	case UAC_VERSION_1:
607 	default:
608 		return set_sample_rate_v1(chip, iface, alts, fmt, rate);
609 
610 	case UAC_VERSION_2:
611 	case UAC_VERSION_3:
612 		return set_sample_rate_v2v3(chip, iface, alts, fmt, rate);
613 	}
614 }
615 
616