clock.c 16.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/*
 *   Clock domain and sample rate management functions
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 *
 */

#include <linux/bitops.h>
#include <linux/init.h>
#include <linux/string.h>
#include <linux/usb.h>
#include <linux/usb/audio.h>
#include <linux/usb/audio-v2.h>
26
#include <linux/usb/audio-v3.h>
27 28 29 30 31 32 33 34

#include <sound/core.h>
#include <sound/info.h>
#include <sound/pcm.h>

#include "usbaudio.h"
#include "card.h"
#include "helper.h"
35
#include "clock.h"
36
#include "quirks.h"
37 38 39 40 41 42 43 44 45 46

static struct uac_clock_source_descriptor *
	snd_usb_find_clock_source(struct usb_host_interface *ctrl_iface,
				  int clock_id)
{
	struct uac_clock_source_descriptor *cs = NULL;

	while ((cs = snd_usb_find_csint_desc(ctrl_iface->extra,
					     ctrl_iface->extralen,
					     cs, UAC2_CLOCK_SOURCE))) {
47
		if (cs->bLength >= sizeof(*cs) && cs->bClockID == clock_id)
48 49 50 51 52 53
			return cs;
	}

	return NULL;
}

54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
static struct uac3_clock_source_descriptor *
	snd_usb_find_clock_source_v3(struct usb_host_interface *ctrl_iface,
				  int clock_id)
{
	struct uac3_clock_source_descriptor *cs = NULL;

	while ((cs = snd_usb_find_csint_desc(ctrl_iface->extra,
					     ctrl_iface->extralen,
					     cs, UAC3_CLOCK_SOURCE))) {
		if (cs->bClockID == clock_id)
			return cs;
	}

	return NULL;
}

70 71 72 73 74 75 76 77 78
static struct uac_clock_selector_descriptor *
	snd_usb_find_clock_selector(struct usb_host_interface *ctrl_iface,
				    int clock_id)
{
	struct uac_clock_selector_descriptor *cs = NULL;

	while ((cs = snd_usb_find_csint_desc(ctrl_iface->extra,
					     ctrl_iface->extralen,
					     cs, UAC2_CLOCK_SELECTOR))) {
79 80 81
		if (cs->bLength >= sizeof(*cs) && cs->bClockID == clock_id) {
			if (cs->bLength < 5 + cs->bNrInPins)
				return NULL;
82
			return cs;
83
		}
84 85 86 87 88
	}

	return NULL;
}

89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
static struct uac3_clock_selector_descriptor *
	snd_usb_find_clock_selector_v3(struct usb_host_interface *ctrl_iface,
				    int clock_id)
{
	struct uac3_clock_selector_descriptor *cs = NULL;

	while ((cs = snd_usb_find_csint_desc(ctrl_iface->extra,
					     ctrl_iface->extralen,
					     cs, UAC3_CLOCK_SELECTOR))) {
		if (cs->bClockID == clock_id)
			return cs;
	}

	return NULL;
}

105 106 107 108 109 110 111 112 113
static struct uac_clock_multiplier_descriptor *
	snd_usb_find_clock_multiplier(struct usb_host_interface *ctrl_iface,
				      int clock_id)
{
	struct uac_clock_multiplier_descriptor *cs = NULL;

	while ((cs = snd_usb_find_csint_desc(ctrl_iface->extra,
					     ctrl_iface->extralen,
					     cs, UAC2_CLOCK_MULTIPLIER))) {
114
		if (cs->bLength >= sizeof(*cs) && cs->bClockID == clock_id)
115 116 117 118 119 120
			return cs;
	}

	return NULL;
}

121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
static struct uac3_clock_multiplier_descriptor *
	snd_usb_find_clock_multiplier_v3(struct usb_host_interface *ctrl_iface,
				      int clock_id)
{
	struct uac3_clock_multiplier_descriptor *cs = NULL;

	while ((cs = snd_usb_find_csint_desc(ctrl_iface->extra,
					     ctrl_iface->extralen,
					     cs, UAC3_CLOCK_MULTIPLIER))) {
		if (cs->bClockID == clock_id)
			return cs;
	}

	return NULL;
}

137 138 139 140 141 142 143 144
static int uac_clock_selector_get_val(struct snd_usb_audio *chip, int selector_id)
{
	unsigned char buf;
	int ret;

	ret = snd_usb_ctl_msg(chip->dev, usb_rcvctrlpipe(chip->dev, 0),
			      UAC2_CS_CUR,
			      USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
145 146
			      UAC2_CX_CLOCK_SELECTOR << 8,
			      snd_usb_ctrl_intf(chip) | (selector_id << 8),
147
			      &buf, sizeof(buf));
148 149 150 151 152 153 154

	if (ret < 0)
		return ret;

	return buf;
}

155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
static int uac_clock_selector_set_val(struct snd_usb_audio *chip, int selector_id,
					unsigned char pin)
{
	int ret;

	ret = snd_usb_ctl_msg(chip->dev, usb_sndctrlpipe(chip->dev, 0),
			      UAC2_CS_CUR,
			      USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
			      UAC2_CX_CLOCK_SELECTOR << 8,
			      snd_usb_ctrl_intf(chip) | (selector_id << 8),
			      &pin, sizeof(pin));
	if (ret < 0)
		return ret;

	if (ret != sizeof(pin)) {
170 171 172
		usb_audio_err(chip,
			"setting selector (id %d) unexpected length %d\n",
			selector_id, ret);
173 174 175 176 177 178 179 180
		return -EINVAL;
	}

	ret = uac_clock_selector_get_val(chip, selector_id);
	if (ret < 0)
		return ret;

	if (ret != pin) {
181 182 183
		usb_audio_err(chip,
			"setting selector (id %d) to %x failed (current: %d)\n",
			selector_id, pin, ret);
184 185 186 187 188 189
		return -EINVAL;
	}

	return ret;
}

190 191 192
static bool uac_clock_source_is_valid(struct snd_usb_audio *chip,
				      int protocol,
				      int source_id)
193 194 195 196
{
	int err;
	unsigned char data;
	struct usb_device *dev = chip->dev;
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
	u32 bmControls;

	if (protocol == UAC_VERSION_3) {
		struct uac3_clock_source_descriptor *cs_desc =
			snd_usb_find_clock_source_v3(chip->ctrl_intf, source_id);

		if (!cs_desc)
			return 0;
		bmControls = le32_to_cpu(cs_desc->bmControls);
	} else { /* UAC_VERSION_1/2 */
		struct uac_clock_source_descriptor *cs_desc =
			snd_usb_find_clock_source(chip->ctrl_intf, source_id);

		if (!cs_desc)
			return 0;
		bmControls = cs_desc->bmControls;
	}
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 221

	err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_CUR,
			      USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
222 223
			      UAC2_CS_CONTROL_CLOCK_VALID << 8,
			      snd_usb_ctrl_intf(chip) | (source_id << 8),
224
			      &data, sizeof(data));
225 226

	if (err < 0) {
227 228
		dev_warn(&dev->dev,
			 "%s(): cannot get clock validity for id %d\n",
229
			   __func__, source_id);
230
		return 0;
231 232 233 234 235
	}

	return !!data;
}

236 237
static int __uac_clock_find_source(struct snd_usb_audio *chip, int entity_id,
				   unsigned long *visited, bool validate)
238 239 240 241 242 243 244 245
{
	struct uac_clock_source_descriptor *source;
	struct uac_clock_selector_descriptor *selector;
	struct uac_clock_multiplier_descriptor *multiplier;

	entity_id &= 0xff;

	if (test_and_set_bit(entity_id, visited)) {
246 247 248
		usb_audio_warn(chip,
			 "%s(): recursive clock topology detected, id %d.\n",
			 __func__, entity_id);
249 250 251 252
		return -EINVAL;
	}

	/* 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 255
	if (source) {
		entity_id = source->bClockID;
256 257
		if (validate && !uac_clock_source_is_valid(chip, UAC_VERSION_2,
								entity_id)) {
258 259 260
			usb_audio_err(chip,
				"clock source %d is not valid, cannot use\n",
				entity_id);
261 262 263 264
			return -ENXIO;
		}
		return entity_id;
	}
265

266
	selector = snd_usb_find_clock_selector(chip->ctrl_intf, entity_id);
267
	if (selector) {
268
		int ret, i, cur;
269 270 271 272 273 274 275

		/* the entity ID we are looking for is a selector.
		 * find out what it currently selects */
		ret = uac_clock_selector_get_val(chip, selector->bClockID);
		if (ret < 0)
			return ret;

276 277
		/* Selector values are one-based */

278
		if (ret > selector->bNrInPins || ret < 1) {
279
			usb_audio_err(chip,
280 281 282 283 284 285
				"%s(): selector reported illegal value, id %d, ret %d\n",
				__func__, selector->bClockID, ret);

			return -EINVAL;
		}

286 287
		cur = ret;
		ret = __uac_clock_find_source(chip, selector->baCSourceID[ret - 1],
288
					       visited, validate);
289
		if (!validate || ret > 0 || !chip->autoclock)
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
			return ret;

		/* The current clock source is invalid, try others. */
		for (i = 1; i <= selector->bNrInPins; i++) {
			int err;

			if (i == cur)
				continue;

			ret = __uac_clock_find_source(chip, selector->baCSourceID[i - 1],
				visited, true);
			if (ret < 0)
				continue;

			err = uac_clock_selector_set_val(chip, entity_id, i);
			if (err < 0)
				continue;

308 309 310
			usb_audio_info(chip,
				 "found and selected valid clock source %d\n",
				 ret);
311 312 313 314
			return ret;
		}

		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 324 325

	return -EINVAL;
}

326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
static int __uac3_clock_find_source(struct snd_usb_audio *chip, int entity_id,
				   unsigned long *visited, bool validate)
{
	struct uac3_clock_source_descriptor *source;
	struct uac3_clock_selector_descriptor *selector;
	struct uac3_clock_multiplier_descriptor *multiplier;

	entity_id &= 0xff;

	if (test_and_set_bit(entity_id, visited)) {
		usb_audio_warn(chip,
			 "%s(): recursive clock topology detected, id %d.\n",
			 __func__, entity_id);
		return -EINVAL;
	}

	/* first, see if the ID we're looking for is a clock source already */
	source = snd_usb_find_clock_source_v3(chip->ctrl_intf, entity_id);
	if (source) {
		entity_id = source->bClockID;
		if (validate && !uac_clock_source_is_valid(chip, UAC_VERSION_3,
								entity_id)) {
			usb_audio_err(chip,
				"clock source %d is not valid, cannot use\n",
				entity_id);
			return -ENXIO;
		}
		return entity_id;
	}

	selector = snd_usb_find_clock_selector_v3(chip->ctrl_intf, entity_id);
	if (selector) {
		int ret, i, cur;

		/* the entity ID we are looking for is a selector.
		 * find out what it currently selects */
		ret = uac_clock_selector_get_val(chip, selector->bClockID);
		if (ret < 0)
			return ret;

		/* Selector values are one-based */

		if (ret > selector->bNrInPins || ret < 1) {
			usb_audio_err(chip,
				"%s(): selector reported illegal value, id %d, ret %d\n",
				__func__, selector->bClockID, ret);

			return -EINVAL;
		}

		cur = ret;
		ret = __uac3_clock_find_source(chip, selector->baCSourceID[ret - 1],
					       visited, validate);
		if (!validate || ret > 0 || !chip->autoclock)
			return ret;

		/* The current clock source is invalid, try others. */
		for (i = 1; i <= selector->bNrInPins; i++) {
			int err;

			if (i == cur)
				continue;

			ret = __uac3_clock_find_source(chip, selector->baCSourceID[i - 1],
				visited, true);
			if (ret < 0)
				continue;

			err = uac_clock_selector_set_val(chip, entity_id, i);
			if (err < 0)
				continue;

			usb_audio_info(chip,
				 "found and selected valid clock source %d\n",
				 ret);
			return ret;
		}

		return -ENXIO;
	}

	/* FIXME: multipliers only act as pass-thru element for now */
	multiplier = snd_usb_find_clock_multiplier_v3(chip->ctrl_intf,
						      entity_id);
	if (multiplier)
		return __uac3_clock_find_source(chip, multiplier->bCSourceID,
						visited, validate);

	return -EINVAL;
}

417 418 419 420 421 422 423 424 425 426 427
/*
 * For all kinds of sample rate settings and other device queries,
 * the clock source (end-leaf) must be used. However, clock selectors,
 * clock multipliers and sample rate converters may be specified as
 * clock source input to terminal. This functions walks the clock path
 * to its end and tries to find the source.
 *
 * The 'visited' bitfield is used internally to detect recursive loops.
 *
 * Returns the clock source UnitID (>=0) on success, or an error.
 */
428 429
int snd_usb_clock_find_source(struct snd_usb_audio *chip, int protocol,
			      int entity_id, bool validate)
430 431 432
{
	DECLARE_BITMAP(visited, 256);
	memset(visited, 0, sizeof(visited));
433 434 435 436 437 438 439 440 441 442 443

	switch (protocol) {
	case UAC_VERSION_2:
		return __uac_clock_find_source(chip, entity_id, visited,
					       validate);
	case UAC_VERSION_3:
		return __uac3_clock_find_source(chip, entity_id, visited,
					       validate);
	default:
		return -EINVAL;
	}
444 445 446 447 448 449 450 451 452 453 454
}

static int set_sample_rate_v1(struct snd_usb_audio *chip, int iface,
			      struct usb_host_interface *alts,
			      struct audioformat *fmt, int rate)
{
	struct usb_device *dev = chip->dev;
	unsigned int ep;
	unsigned char data[3];
	int err, crate;

455 456
	if (get_iface_desc(alts)->bNumEndpoints < 1)
		return -EINVAL;
457 458 459
	ep = get_endpoint(alts, 0)->bEndpointAddress;

	/* if endpoint doesn't have sampling rate control, bail out */
460
	if (!(fmt->attributes & UAC_EP_CS_ATTR_SAMPLE_RATE))
461 462 463 464 465 466 467 468
		return 0;

	data[0] = rate;
	data[1] = rate >> 8;
	data[2] = rate >> 16;
	if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR,
				   USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_OUT,
				   UAC_EP_CS_ATTR_SAMPLE_RATE << 8, ep,
469
				   data, sizeof(data))) < 0) {
470 471
		dev_err(&dev->dev, "%d:%d: cannot set freq %d to ep %#x\n",
			iface, fmt->altsetting, rate, ep);
472 473 474
		return err;
	}

475 476 477 478
	/* Don't check the sample rate for devices which we know don't
	 * support reading */
	if (snd_usb_get_sample_rate_quirk(chip))
		return 0;
479 480 481
	/* the firmware is likely buggy, don't repeat to fail too many times */
	if (chip->sample_rate_read_error > 2)
		return 0;
482

483 484 485
	if ((err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC_GET_CUR,
				   USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_IN,
				   UAC_EP_CS_ATTR_SAMPLE_RATE << 8, ep,
486
				   data, sizeof(data))) < 0) {
487 488
		dev_err(&dev->dev, "%d:%d: cannot get freq at ep %#x\n",
			iface, fmt->altsetting, ep);
489
		chip->sample_rate_read_error++;
490 491 492 493 494
		return 0; /* some devices don't support reading */
	}

	crate = data[0] | (data[1] << 8) | (data[2] << 16);
	if (crate != rate) {
495
		dev_warn(&dev->dev, "current rate %d is different from the runtime rate %d\n", crate, rate);
496 497 498 499 500 501
		// runtime->rate = crate;
	}

	return 0;
}

502
static int get_sample_rate_v2v3(struct snd_usb_audio *chip, int iface,
503 504 505
			      int altsetting, int clock)
{
	struct usb_device *dev = chip->dev;
506
	__le32 data;
507 508 509 510 511 512
	int err;

	err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_CUR,
			      USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
			      UAC2_CS_CONTROL_SAM_FREQ << 8,
			      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 518 519
		return 0;
	}

520
	return le32_to_cpu(data);
521 522
}

523
static int set_sample_rate_v2v3(struct snd_usb_audio *chip, int iface,
524 525 526 527
			      struct usb_host_interface *alts,
			      struct audioformat *fmt, int rate)
{
	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 535
	clock = snd_usb_clock_find_source(chip, fmt->protocol,
					  fmt->clock, true);
536 537 538
	if (clock < 0)
		return clock;

539
	prev_rate = get_sample_rate_v2v3(chip, iface, fmt->altsetting, clock);
540 541
	if (prev_rate == rate)
		return 0;
542

543 544 545 546 547 548 549 550 551 552 553 554
	if (fmt->protocol == UAC_VERSION_3) {
		struct uac3_clock_source_descriptor *cs_desc;

		cs_desc = snd_usb_find_clock_source_v3(chip->ctrl_intf, clock);
		bmControls = le32_to_cpu(cs_desc->bmControls);
	} else {
		struct uac_clock_source_descriptor *cs_desc;

		cs_desc = snd_usb_find_clock_source(chip->ctrl_intf, clock);
		bmControls = cs_desc->bmControls;
	}

555 556
	writeable = uac_v2v3_control_is_writeable(bmControls,
						  UAC2_CS_CONTROL_SAM_FREQ);
557 558 559 560 561 562 563 564
	if (writeable) {
		data = cpu_to_le32(rate);
		err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC2_CS_CUR,
				      USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
				      UAC2_CS_CONTROL_SAM_FREQ << 8,
				      snd_usb_ctrl_intf(chip) | (clock << 8),
				      &data, sizeof(data));
		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 569
			return err;
		}
570

571 572
		cur_rate = get_sample_rate_v2v3(chip, iface,
						fmt->altsetting, clock);
573 574 575
	} else {
		cur_rate = prev_rate;
	}
576

577
	if (cur_rate != rate) {
578
		if (!writeable) {
579 580 581
			usb_audio_warn(chip,
				 "%d:%d: freq mismatch (RO clock): req %d, clock runs @%d\n",
				 iface, fmt->altsetting, rate, cur_rate);
582 583
			return -ENXIO;
		}
584 585 586
		usb_audio_dbg(chip,
			"current rate %d is different from the runtime rate %d\n",
			cur_rate, rate);
587 588 589 590 591 592
	}

	/* Some devices doesn't respond to sample rate changes while the
	 * interface is active. */
	if (rate != prev_rate) {
		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 599 600 601 602 603 604

	return 0;
}

int snd_usb_init_sample_rate(struct snd_usb_audio *chip, int iface,
			     struct usb_host_interface *alts,
			     struct audioformat *fmt, int rate)
{
605
	switch (fmt->protocol) {
606
	case UAC_VERSION_1:
607
	default:
608 609 610
		return set_sample_rate_v1(chip, iface, alts, fmt, rate);

	case UAC_VERSION_2:
611 612
	case UAC_VERSION_3:
		return set_sample_rate_v2v3(chip, iface, alts, fmt, rate);
613 614 615
	}
}