Linux Audio

Check our new training course

Loading...
v6.2
   1/****************************************************************************
   2
   3   Copyright Echo Digital Audio Corporation (c) 1998 - 2004
   4   All rights reserved
   5   www.echoaudio.com
   6
   7   This file is part of Echo Digital Audio's generic driver library.
   8
   9   Echo Digital Audio's generic driver library is free software;
  10   you can redistribute it and/or modify it under the terms of
  11   the GNU General Public License as published by the Free Software
  12   Foundation.
  13
  14   This program is distributed in the hope that it will be useful,
  15   but WITHOUT ANY WARRANTY; without even the implied warranty of
  16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17   GNU General Public License for more details.
  18
  19   You should have received a copy of the GNU General Public License
  20   along with this program; if not, write to the Free Software
  21   Foundation, Inc., 59 Temple Place - Suite 330, Boston,
  22   MA  02111-1307, USA.
  23
  24   *************************************************************************
  25
  26 Translation from C++ and adaptation for use in ALSA-Driver
  27 were made by Giuliano Pochini <pochini@shiny.it>
  28
  29****************************************************************************/
  30
  31#if PAGE_SIZE < 4096
  32#error PAGE_SIZE is < 4k
  33#endif
  34
  35static int restore_dsp_rettings(struct echoaudio *chip);
  36
  37
  38/* Some vector commands involve the DSP reading or writing data to and from the
  39comm page; if you send one of these commands to the DSP, it will complete the
  40command and then write a non-zero value to the Handshake field in the
  41comm page.  This function waits for the handshake to show up. */
  42static int wait_handshake(struct echoaudio *chip)
  43{
  44	int i;
  45
  46	/* Wait up to 20ms for the handshake from the DSP */
  47	for (i = 0; i < HANDSHAKE_TIMEOUT; i++) {
  48		/* Look for the handshake value */
  49		barrier();
  50		if (chip->comm_page->handshake) {
  51			return 0;
  52		}
  53		udelay(1);
  54	}
  55
  56	dev_err(chip->card->dev, "wait_handshake(): Timeout waiting for DSP\n");
  57	return -EBUSY;
  58}
  59
  60
  61
  62/* Much of the interaction between the DSP and the driver is done via vector
  63commands; send_vector writes a vector command to the DSP.  Typically, this
  64causes the DSP to read or write fields in the comm page.
  65PCI posting is not required thanks to the handshake logic. */
  66static int send_vector(struct echoaudio *chip, u32 command)
  67{
  68	int i;
  69
  70	wmb();	/* Flush all pending writes before sending the command */
  71
  72	/* Wait up to 100ms for the "vector busy" bit to be off */
  73	for (i = 0; i < VECTOR_BUSY_TIMEOUT; i++) {
  74		if (!(get_dsp_register(chip, CHI32_VECTOR_REG) &
  75		      CHI32_VECTOR_BUSY)) {
  76			set_dsp_register(chip, CHI32_VECTOR_REG, command);
  77			/*if (i)  DE_ACT(("send_vector time: %d\n", i));*/
  78			return 0;
  79		}
  80		udelay(1);
  81	}
  82
  83	dev_err(chip->card->dev, "timeout on send_vector\n");
  84	return -EBUSY;
  85}
  86
  87
  88
  89/* write_dsp writes a 32-bit value to the DSP; this is used almost
  90exclusively for loading the DSP. */
  91static int write_dsp(struct echoaudio *chip, u32 data)
  92{
  93	u32 status, i;
  94
  95	for (i = 0; i < 10000000; i++) {	/* timeout = 10s */
  96		status = get_dsp_register(chip, CHI32_STATUS_REG);
  97		if ((status & CHI32_STATUS_HOST_WRITE_EMPTY) != 0) {
  98			set_dsp_register(chip, CHI32_DATA_REG, data);
  99			wmb();			/* write it immediately */
 100			return 0;
 101		}
 102		udelay(1);
 103		cond_resched();
 104	}
 105
 106	chip->bad_board = true;		/* Set true until DSP re-loaded */
 107	dev_dbg(chip->card->dev, "write_dsp: Set bad_board to true\n");
 108	return -EIO;
 109}
 110
 111
 112
 113/* read_dsp reads a 32-bit value from the DSP; this is used almost
 114exclusively for loading the DSP and checking the status of the ASIC. */
 115static int read_dsp(struct echoaudio *chip, u32 *data)
 116{
 117	u32 status, i;
 118
 119	for (i = 0; i < READ_DSP_TIMEOUT; i++) {
 120		status = get_dsp_register(chip, CHI32_STATUS_REG);
 121		if ((status & CHI32_STATUS_HOST_READ_FULL) != 0) {
 122			*data = get_dsp_register(chip, CHI32_DATA_REG);
 123			return 0;
 124		}
 125		udelay(1);
 126		cond_resched();
 127	}
 128
 129	chip->bad_board = true;		/* Set true until DSP re-loaded */
 130	dev_err(chip->card->dev, "read_dsp: Set bad_board to true\n");
 131	return -EIO;
 132}
 133
 134
 135
 136/****************************************************************************
 137	Firmware loading functions
 138 ****************************************************************************/
 139
 140/* This function is used to read back the serial number from the DSP;
 141this is triggered by the SET_COMMPAGE_ADDR command.
 142Only some early Echogals products have serial numbers in the ROM;
 143the serial number is not used, but you still need to do this as
 144part of the DSP load process. */
 145static int read_sn(struct echoaudio *chip)
 146{
 147	int i;
 148	u32 sn[6];
 149
 150	for (i = 0; i < 5; i++) {
 151		if (read_dsp(chip, &sn[i])) {
 152			dev_err(chip->card->dev,
 153				"Failed to read serial number\n");
 154			return -EIO;
 155		}
 156	}
 157	dev_dbg(chip->card->dev,
 158		"Read serial number %08x %08x %08x %08x %08x\n",
 159		 sn[0], sn[1], sn[2], sn[3], sn[4]);
 160	return 0;
 161}
 162
 163
 164
 165#ifndef ECHOCARD_HAS_ASIC
 166/* This card has no ASIC, just return ok */
 167static inline int check_asic_status(struct echoaudio *chip)
 168{
 169	chip->asic_loaded = true;
 170	return 0;
 171}
 172
 173#endif /* !ECHOCARD_HAS_ASIC */
 174
 175
 176
 177#ifdef ECHOCARD_HAS_ASIC
 178
 179/* Load ASIC code - done after the DSP is loaded */
 180static int load_asic_generic(struct echoaudio *chip, u32 cmd, short asic)
 181{
 182	const struct firmware *fw;
 183	int err;
 184	u32 i, size;
 185	u8 *code;
 186
 187	err = get_firmware(&fw, chip, asic);
 188	if (err < 0) {
 189		dev_warn(chip->card->dev, "Firmware not found !\n");
 190		return err;
 191	}
 192
 193	code = (u8 *)fw->data;
 194	size = fw->size;
 195
 196	/* Send the "Here comes the ASIC" command */
 197	if (write_dsp(chip, cmd) < 0)
 198		goto la_error;
 199
 200	/* Write length of ASIC file in bytes */
 201	if (write_dsp(chip, size) < 0)
 202		goto la_error;
 203
 204	for (i = 0; i < size; i++) {
 205		if (write_dsp(chip, code[i]) < 0)
 206			goto la_error;
 207	}
 208
 209	free_firmware(fw, chip);
 210	return 0;
 211
 212la_error:
 213	dev_err(chip->card->dev, "failed on write_dsp\n");
 214	free_firmware(fw, chip);
 215	return -EIO;
 216}
 217
 218#endif /* ECHOCARD_HAS_ASIC */
 219
 220
 221
 222#ifdef DSP_56361
 223
 224/* Install the resident loader for 56361 DSPs;  The resident loader is on
 225the EPROM on the board for 56301 DSP. The resident loader is a tiny little
 226program that is used to load the real DSP code. */
 227static int install_resident_loader(struct echoaudio *chip)
 228{
 229	u32 address;
 230	int index, words, i;
 231	u16 *code;
 232	u32 status;
 233	const struct firmware *fw;
 234
 235	/* 56361 cards only!  This check is required by the old 56301-based
 236	Mona and Gina24 */
 237	if (chip->device_id != DEVICE_ID_56361)
 238		return 0;
 239
 240	/* Look to see if the resident loader is present.  If the resident
 241	loader is already installed, host flag 5 will be on. */
 242	status = get_dsp_register(chip, CHI32_STATUS_REG);
 243	if (status & CHI32_STATUS_REG_HF5) {
 244		dev_dbg(chip->card->dev,
 245			"Resident loader already installed; status is 0x%x\n",
 246			 status);
 247		return 0;
 248	}
 249
 250	i = get_firmware(&fw, chip, FW_361_LOADER);
 251	if (i < 0) {
 252		dev_warn(chip->card->dev, "Firmware not found !\n");
 253		return i;
 254	}
 255
 256	/* The DSP code is an array of 16 bit words.  The array is divided up
 257	into sections.  The first word of each section is the size in words,
 258	followed by the section type.
 259	Since DSP addresses and data are 24 bits wide, they each take up two
 260	16 bit words in the array.
 261	This is a lot like the other loader loop, but it's not a loop, you
 262	don't write the memory type, and you don't write a zero at the end. */
 263
 264	/* Set DSP format bits for 24 bit mode */
 265	set_dsp_register(chip, CHI32_CONTROL_REG,
 266			 get_dsp_register(chip, CHI32_CONTROL_REG) | 0x900);
 267
 268	code = (u16 *)fw->data;
 269
 270	/* Skip the header section; the first word in the array is the size
 271	of the first section, so the first real section of code is pointed
 272	to by Code[0]. */
 273	index = code[0];
 274
 275	/* Skip the section size, LRS block type, and DSP memory type */
 276	index += 3;
 277
 278	/* Get the number of DSP words to write */
 279	words = code[index++];
 280
 281	/* Get the DSP address for this block; 24 bits, so build from two words */
 282	address = ((u32)code[index] << 16) + code[index + 1];
 283	index += 2;
 284
 285	/* Write the count to the DSP */
 286	if (write_dsp(chip, words)) {
 287		dev_err(chip->card->dev,
 288			"install_resident_loader: Failed to write word count!\n");
 289		goto irl_error;
 290	}
 291	/* Write the DSP address */
 292	if (write_dsp(chip, address)) {
 293		dev_err(chip->card->dev,
 294			"install_resident_loader: Failed to write DSP address!\n");
 295		goto irl_error;
 296	}
 297	/* Write out this block of code to the DSP */
 298	for (i = 0; i < words; i++) {
 299		u32 data;
 300
 301		data = ((u32)code[index] << 16) + code[index + 1];
 302		if (write_dsp(chip, data)) {
 303			dev_err(chip->card->dev,
 304				"install_resident_loader: Failed to write DSP code\n");
 305			goto irl_error;
 306		}
 307		index += 2;
 308	}
 309
 310	/* Wait for flag 5 to come up */
 311	for (i = 0; i < 200; i++) {	/* Timeout is 50us * 200 = 10ms */
 312		udelay(50);
 313		status = get_dsp_register(chip, CHI32_STATUS_REG);
 314		if (status & CHI32_STATUS_REG_HF5)
 315			break;
 316	}
 317
 318	if (i == 200) {
 319		dev_err(chip->card->dev, "Resident loader failed to set HF5\n");
 320		goto irl_error;
 321	}
 322
 323	dev_dbg(chip->card->dev, "Resident loader successfully installed\n");
 324	free_firmware(fw, chip);
 325	return 0;
 326
 327irl_error:
 328	free_firmware(fw, chip);
 329	return -EIO;
 330}
 331
 332#endif /* DSP_56361 */
 333
 334
 335static int load_dsp(struct echoaudio *chip, u16 *code)
 336{
 337	u32 address, data;
 338	int index, words, i;
 339
 340	if (chip->dsp_code == code) {
 341		dev_warn(chip->card->dev, "DSP is already loaded!\n");
 342		return 0;
 343	}
 344	chip->bad_board = true;		/* Set true until DSP loaded */
 345	chip->dsp_code = NULL;		/* Current DSP code not loaded */
 346	chip->asic_loaded = false;	/* Loading the DSP code will reset the ASIC */
 347
 348	dev_dbg(chip->card->dev, "load_dsp: Set bad_board to true\n");
 349
 350	/* If this board requires a resident loader, install it. */
 351#ifdef DSP_56361
 352	i = install_resident_loader(chip);
 353	if (i < 0)
 354		return i;
 355#endif
 356
 357	/* Send software reset command */
 358	if (send_vector(chip, DSP_VC_RESET) < 0) {
 359		dev_err(chip->card->dev,
 360			"LoadDsp: send_vector DSP_VC_RESET failed, Critical Failure\n");
 361		return -EIO;
 362	}
 363	/* Delay 10us */
 364	udelay(10);
 365
 366	/* Wait 10ms for HF3 to indicate that software reset is complete */
 367	for (i = 0; i < 1000; i++) {	/* Timeout is 10us * 1000 = 10ms */
 368		if (get_dsp_register(chip, CHI32_STATUS_REG) &
 369		    CHI32_STATUS_REG_HF3)
 370			break;
 371		udelay(10);
 372	}
 373
 374	if (i == 1000) {
 375		dev_err(chip->card->dev,
 376			"load_dsp: Timeout waiting for CHI32_STATUS_REG_HF3\n");
 377		return -EIO;
 378	}
 379
 380	/* Set DSP format bits for 24 bit mode now that soft reset is done */
 381	set_dsp_register(chip, CHI32_CONTROL_REG,
 382			 get_dsp_register(chip, CHI32_CONTROL_REG) | 0x900);
 383
 384	/* Main loader loop */
 385
 386	index = code[0];
 387	for (;;) {
 388		int block_type, mem_type;
 389
 390		/* Total Block Size */
 391		index++;
 392
 393		/* Block Type */
 394		block_type = code[index];
 395		if (block_type == 4)	/* We're finished */
 396			break;
 397
 398		index++;
 399
 400		/* Memory Type  P=0,X=1,Y=2 */
 401		mem_type = code[index++];
 402
 403		/* Block Code Size */
 404		words = code[index++];
 405		if (words == 0)		/* We're finished */
 406			break;
 407
 408		/* Start Address */
 409		address = ((u32)code[index] << 16) + code[index + 1];
 410		index += 2;
 411
 412		if (write_dsp(chip, words) < 0) {
 413			dev_err(chip->card->dev,
 414				"load_dsp: failed to write number of DSP words\n");
 415			return -EIO;
 416		}
 417		if (write_dsp(chip, address) < 0) {
 418			dev_err(chip->card->dev,
 419				"load_dsp: failed to write DSP address\n");
 420			return -EIO;
 421		}
 422		if (write_dsp(chip, mem_type) < 0) {
 423			dev_err(chip->card->dev,
 424				"load_dsp: failed to write DSP memory type\n");
 425			return -EIO;
 426		}
 427		/* Code */
 428		for (i = 0; i < words; i++, index+=2) {
 429			data = ((u32)code[index] << 16) + code[index + 1];
 430			if (write_dsp(chip, data) < 0) {
 431				dev_err(chip->card->dev,
 432					"load_dsp: failed to write DSP data\n");
 433				return -EIO;
 434			}
 435		}
 436	}
 437
 438	if (write_dsp(chip, 0) < 0) {	/* We're done!!! */
 439		dev_err(chip->card->dev,
 440			"load_dsp: Failed to write final zero\n");
 441		return -EIO;
 442	}
 443	udelay(10);
 444
 445	for (i = 0; i < 5000; i++) {	/* Timeout is 100us * 5000 = 500ms */
 446		/* Wait for flag 4 - indicates that the DSP loaded OK */
 447		if (get_dsp_register(chip, CHI32_STATUS_REG) &
 448		    CHI32_STATUS_REG_HF4) {
 449			set_dsp_register(chip, CHI32_CONTROL_REG,
 450					 get_dsp_register(chip, CHI32_CONTROL_REG) & ~0x1b00);
 451
 452			if (write_dsp(chip, DSP_FNC_SET_COMMPAGE_ADDR) < 0) {
 453				dev_err(chip->card->dev,
 454					"load_dsp: Failed to write DSP_FNC_SET_COMMPAGE_ADDR\n");
 455				return -EIO;
 456			}
 457
 458			if (write_dsp(chip, chip->comm_page_phys) < 0) {
 459				dev_err(chip->card->dev,
 460					"load_dsp: Failed to write comm page address\n");
 461				return -EIO;
 462			}
 463
 464			/* Get the serial number via slave mode.
 465			This is triggered by the SET_COMMPAGE_ADDR command.
 466			We don't actually use the serial number but we have to
 467			get it as part of the DSP init voodoo. */
 468			if (read_sn(chip) < 0) {
 469				dev_err(chip->card->dev,
 470					"load_dsp: Failed to read serial number\n");
 471				return -EIO;
 472			}
 473
 474			chip->dsp_code = code;		/* Show which DSP code loaded */
 475			chip->bad_board = false;	/* DSP OK */
 476			return 0;
 477		}
 478		udelay(100);
 479	}
 480
 481	dev_err(chip->card->dev,
 482		"load_dsp: DSP load timed out waiting for HF4\n");
 483	return -EIO;
 484}
 485
 486
 487
 488/* load_firmware takes care of loading the DSP and any ASIC code. */
 489static int load_firmware(struct echoaudio *chip)
 490{
 491	const struct firmware *fw;
 492	int box_type, err;
 493
 494	if (snd_BUG_ON(!chip->comm_page))
 495		return -EPERM;
 496
 497	/* See if the ASIC is present and working - only if the DSP is already loaded */
 498	if (chip->dsp_code) {
 499		box_type = check_asic_status(chip);
 500		if (box_type >= 0)
 501			return box_type;
 502		/* ASIC check failed; force the DSP to reload */
 503		chip->dsp_code = NULL;
 504	}
 505
 506	err = get_firmware(&fw, chip, chip->dsp_code_to_load);
 507	if (err < 0)
 508		return err;
 509	err = load_dsp(chip, (u16 *)fw->data);
 510	free_firmware(fw, chip);
 511	if (err < 0)
 512		return err;
 513
 514	box_type = load_asic(chip);
 515	if (box_type < 0)
 516		return box_type;	/* error */
 517
 518	return box_type;
 519}
 520
 521
 522
 523/****************************************************************************
 524	Mixer functions
 525 ****************************************************************************/
 526
 527#if defined(ECHOCARD_HAS_INPUT_NOMINAL_LEVEL) || \
 528	defined(ECHOCARD_HAS_OUTPUT_NOMINAL_LEVEL)
 529
 530/* Set the nominal level for an input or output bus (true = -10dBV, false = +4dBu) */
 531static int set_nominal_level(struct echoaudio *chip, u16 index, char consumer)
 532{
 533	if (snd_BUG_ON(index >= num_busses_out(chip) + num_busses_in(chip)))
 534		return -EINVAL;
 535
 536	/* Wait for the handshake (OK even if ASIC is not loaded) */
 537	if (wait_handshake(chip))
 538		return -EIO;
 539
 540	chip->nominal_level[index] = consumer;
 541
 542	if (consumer)
 543		chip->comm_page->nominal_level_mask |= cpu_to_le32(1 << index);
 544	else
 545		chip->comm_page->nominal_level_mask &= ~cpu_to_le32(1 << index);
 546
 547	return 0;
 548}
 549
 550#endif /* ECHOCARD_HAS_*_NOMINAL_LEVEL */
 551
 552
 553
 554/* Set the gain for a single physical output channel (dB). */
 555static int set_output_gain(struct echoaudio *chip, u16 channel, s8 gain)
 556{
 557	if (snd_BUG_ON(channel >= num_busses_out(chip)))
 558		return -EINVAL;
 559
 560	if (wait_handshake(chip))
 561		return -EIO;
 562
 563	/* Save the new value */
 564	chip->output_gain[channel] = gain;
 565	chip->comm_page->line_out_level[channel] = gain;
 566	return 0;
 567}
 568
 569
 570
 571#ifdef ECHOCARD_HAS_MONITOR
 572/* Set the monitor level from an input bus to an output bus. */
 573static int set_monitor_gain(struct echoaudio *chip, u16 output, u16 input,
 574			    s8 gain)
 575{
 576	if (snd_BUG_ON(output >= num_busses_out(chip) ||
 577		    input >= num_busses_in(chip)))
 578		return -EINVAL;
 579
 580	if (wait_handshake(chip))
 581		return -EIO;
 582
 583	chip->monitor_gain[output][input] = gain;
 584	chip->comm_page->monitors[monitor_index(chip, output, input)] = gain;
 585	return 0;
 586}
 587#endif /* ECHOCARD_HAS_MONITOR */
 588
 589
 590/* Tell the DSP to read and update output, nominal & monitor levels in comm page. */
 591static int update_output_line_level(struct echoaudio *chip)
 592{
 593	if (wait_handshake(chip))
 594		return -EIO;
 595	clear_handshake(chip);
 596	return send_vector(chip, DSP_VC_UPDATE_OUTVOL);
 597}
 598
 599
 600
 601/* Tell the DSP to read and update input levels in comm page */
 602static int update_input_line_level(struct echoaudio *chip)
 603{
 604	if (wait_handshake(chip))
 605		return -EIO;
 606	clear_handshake(chip);
 607	return send_vector(chip, DSP_VC_UPDATE_INGAIN);
 608}
 609
 610
 611
 612/* set_meters_on turns the meters on or off.  If meters are turned on, the DSP
 613will write the meter and clock detect values to the comm page at about 30Hz */
 614static void set_meters_on(struct echoaudio *chip, char on)
 615{
 616	if (on && !chip->meters_enabled) {
 617		send_vector(chip, DSP_VC_METERS_ON);
 618		chip->meters_enabled = 1;
 619	} else if (!on && chip->meters_enabled) {
 620		send_vector(chip, DSP_VC_METERS_OFF);
 621		chip->meters_enabled = 0;
 622		memset((s8 *)chip->comm_page->vu_meter, ECHOGAIN_MUTED,
 623		       DSP_MAXPIPES);
 624		memset((s8 *)chip->comm_page->peak_meter, ECHOGAIN_MUTED,
 625		       DSP_MAXPIPES);
 626	}
 627}
 628
 629
 630
 631/* Fill out an the given array using the current values in the comm page.
 632Meters are written in the comm page by the DSP in this order:
 633 Output busses
 634 Input busses
 635 Output pipes (vmixer cards only)
 636
 637This function assumes there are no more than 16 in/out busses or pipes
 638Meters is an array [3][16][2] of long. */
 639static void get_audio_meters(struct echoaudio *chip, long *meters)
 640{
 641	unsigned int i, m, n;
 642
 643	for (i = 0 ; i < 96; i++)
 644		meters[i] = 0;
 645
 646	for (m = 0, n = 0, i = 0; i < num_busses_out(chip); i++, m++) {
 647		meters[n++] = chip->comm_page->vu_meter[m];
 648		meters[n++] = chip->comm_page->peak_meter[m];
 649	}
 650
 651#ifdef ECHOCARD_ECHO3G
 652	m = E3G_MAX_OUTPUTS;	/* Skip unused meters */
 653#endif
 654
 655	for (n = 32, i = 0; i < num_busses_in(chip); i++, m++) {
 656		meters[n++] = chip->comm_page->vu_meter[m];
 657		meters[n++] = chip->comm_page->peak_meter[m];
 658	}
 659#ifdef ECHOCARD_HAS_VMIXER
 660	for (n = 64, i = 0; i < num_pipes_out(chip); i++, m++) {
 661		meters[n++] = chip->comm_page->vu_meter[m];
 662		meters[n++] = chip->comm_page->peak_meter[m];
 663	}
 664#endif
 665}
 666
 667
 668
 669static int restore_dsp_rettings(struct echoaudio *chip)
 670{
 671	int i, o, err;
 672
 673	err = check_asic_status(chip);
 674	if (err < 0)
 675		return err;
 676
 677	/* Gina20/Darla20 only. Should be harmless for other cards. */
 678	chip->comm_page->gd_clock_state = GD_CLOCK_UNDEF;
 679	chip->comm_page->gd_spdif_status = GD_SPDIF_STATUS_UNDEF;
 680	chip->comm_page->handshake = cpu_to_le32(0xffffffff);
 681
 682	/* Restore output busses */
 683	for (i = 0; i < num_busses_out(chip); i++) {
 684		err = set_output_gain(chip, i, chip->output_gain[i]);
 685		if (err < 0)
 686			return err;
 687	}
 688
 689#ifdef ECHOCARD_HAS_VMIXER
 690	for (i = 0; i < num_pipes_out(chip); i++)
 691		for (o = 0; o < num_busses_out(chip); o++) {
 692			err = set_vmixer_gain(chip, o, i,
 693						chip->vmixer_gain[o][i]);
 694			if (err < 0)
 695				return err;
 696		}
 697	if (update_vmixer_level(chip) < 0)
 698		return -EIO;
 699#endif /* ECHOCARD_HAS_VMIXER */
 700
 701#ifdef ECHOCARD_HAS_MONITOR
 702	for (o = 0; o < num_busses_out(chip); o++)
 703		for (i = 0; i < num_busses_in(chip); i++) {
 704			err = set_monitor_gain(chip, o, i,
 705						chip->monitor_gain[o][i]);
 706			if (err < 0)
 707				return err;
 708		}
 709#endif /* ECHOCARD_HAS_MONITOR */
 710
 711#ifdef ECHOCARD_HAS_INPUT_GAIN
 712	for (i = 0; i < num_busses_in(chip); i++) {
 713		err = set_input_gain(chip, i, chip->input_gain[i]);
 714		if (err < 0)
 715			return err;
 716	}
 717#endif /* ECHOCARD_HAS_INPUT_GAIN */
 718
 719	err = update_output_line_level(chip);
 720	if (err < 0)
 721		return err;
 722
 723	err = update_input_line_level(chip);
 724	if (err < 0)
 725		return err;
 726
 727	err = set_sample_rate(chip, chip->sample_rate);
 728	if (err < 0)
 729		return err;
 730
 731	if (chip->meters_enabled) {
 732		err = send_vector(chip, DSP_VC_METERS_ON);
 733		if (err < 0)
 734			return err;
 735	}
 736
 737#ifdef ECHOCARD_HAS_DIGITAL_MODE_SWITCH
 738	if (set_digital_mode(chip, chip->digital_mode) < 0)
 739		return -EIO;
 740#endif
 741
 742#ifdef ECHOCARD_HAS_DIGITAL_IO
 743	if (set_professional_spdif(chip, chip->professional_spdif) < 0)
 744		return -EIO;
 745#endif
 746
 747#ifdef ECHOCARD_HAS_PHANTOM_POWER
 748	if (set_phantom_power(chip, chip->phantom_power) < 0)
 749		return -EIO;
 750#endif
 751
 752#ifdef ECHOCARD_HAS_EXTERNAL_CLOCK
 753	/* set_input_clock() also restores automute setting */
 754	if (set_input_clock(chip, chip->input_clock) < 0)
 755		return -EIO;
 756#endif
 757
 758#ifdef ECHOCARD_HAS_OUTPUT_CLOCK_SWITCH
 759	if (set_output_clock(chip, chip->output_clock) < 0)
 760		return -EIO;
 761#endif
 762
 763	if (wait_handshake(chip) < 0)
 764		return -EIO;
 765	clear_handshake(chip);
 766	if (send_vector(chip, DSP_VC_UPDATE_FLAGS) < 0)
 767		return -EIO;
 768
 769	return 0;
 770}
 771
 772
 773
 774/****************************************************************************
 775	Transport functions
 776 ****************************************************************************/
 777
 778/* set_audio_format() sets the format of the audio data in host memory for
 779this pipe.  Note that _MS_ (mono-to-stereo) playback modes are not used by ALSA
 780but they are here because they are just mono while capturing */
 781static void set_audio_format(struct echoaudio *chip, u16 pipe_index,
 782			     const struct audioformat *format)
 783{
 784	u16 dsp_format;
 785
 786	dsp_format = DSP_AUDIOFORM_SS_16LE;
 787
 788	/* Look for super-interleave (no big-endian and 8 bits) */
 789	if (format->interleave > 2) {
 790		switch (format->bits_per_sample) {
 791		case 16:
 792			dsp_format = DSP_AUDIOFORM_SUPER_INTERLEAVE_16LE;
 793			break;
 794		case 24:
 795			dsp_format = DSP_AUDIOFORM_SUPER_INTERLEAVE_24LE;
 796			break;
 797		case 32:
 798			dsp_format = DSP_AUDIOFORM_SUPER_INTERLEAVE_32LE;
 799			break;
 800		}
 801		dsp_format |= format->interleave;
 802	} else if (format->data_are_bigendian) {
 803		/* For big-endian data, only 32 bit samples are supported */
 804		switch (format->interleave) {
 805		case 1:
 806			dsp_format = DSP_AUDIOFORM_MM_32BE;
 807			break;
 808#ifdef ECHOCARD_HAS_STEREO_BIG_ENDIAN32
 809		case 2:
 810			dsp_format = DSP_AUDIOFORM_SS_32BE;
 811			break;
 812#endif
 813		}
 814	} else if (format->interleave == 1 &&
 815		   format->bits_per_sample == 32 && !format->mono_to_stereo) {
 816		/* 32 bit little-endian mono->mono case */
 817		dsp_format = DSP_AUDIOFORM_MM_32LE;
 818	} else {
 819		/* Handle the other little-endian formats */
 820		switch (format->bits_per_sample) {
 821		case 8:
 822			if (format->interleave == 2)
 823				dsp_format = DSP_AUDIOFORM_SS_8;
 824			else
 825				dsp_format = DSP_AUDIOFORM_MS_8;
 826			break;
 827		default:
 828		case 16:
 829			if (format->interleave == 2)
 830				dsp_format = DSP_AUDIOFORM_SS_16LE;
 831			else
 832				dsp_format = DSP_AUDIOFORM_MS_16LE;
 833			break;
 834		case 24:
 835			if (format->interleave == 2)
 836				dsp_format = DSP_AUDIOFORM_SS_24LE;
 837			else
 838				dsp_format = DSP_AUDIOFORM_MS_24LE;
 839			break;
 840		case 32:
 841			if (format->interleave == 2)
 842				dsp_format = DSP_AUDIOFORM_SS_32LE;
 843			else
 844				dsp_format = DSP_AUDIOFORM_MS_32LE;
 845			break;
 846		}
 847	}
 848	dev_dbg(chip->card->dev,
 849		 "set_audio_format[%d] = %x\n", pipe_index, dsp_format);
 850	chip->comm_page->audio_format[pipe_index] = cpu_to_le16(dsp_format);
 851}
 852
 853
 854
 855/* start_transport starts transport for a set of pipes.
 856The bits 1 in channel_mask specify what pipes to start. Only the bit of the
 857first channel must be set, regardless its interleave.
 858Same thing for pause_ and stop_ -trasport below. */
 859static int start_transport(struct echoaudio *chip, u32 channel_mask,
 860			   u32 cyclic_mask)
 861{
 862
 863	if (wait_handshake(chip))
 864		return -EIO;
 865
 866	chip->comm_page->cmd_start |= cpu_to_le32(channel_mask);
 867
 868	if (chip->comm_page->cmd_start) {
 869		clear_handshake(chip);
 870		send_vector(chip, DSP_VC_START_TRANSFER);
 871		if (wait_handshake(chip))
 872			return -EIO;
 873		/* Keep track of which pipes are transporting */
 874		chip->active_mask |= channel_mask;
 875		chip->comm_page->cmd_start = 0;
 876		return 0;
 877	}
 878
 879	dev_err(chip->card->dev, "start_transport: No pipes to start!\n");
 880	return -EINVAL;
 881}
 882
 883
 884
 885static int pause_transport(struct echoaudio *chip, u32 channel_mask)
 886{
 887
 888	if (wait_handshake(chip))
 889		return -EIO;
 890
 891	chip->comm_page->cmd_stop |= cpu_to_le32(channel_mask);
 892	chip->comm_page->cmd_reset = 0;
 893	if (chip->comm_page->cmd_stop) {
 894		clear_handshake(chip);
 895		send_vector(chip, DSP_VC_STOP_TRANSFER);
 896		if (wait_handshake(chip))
 897			return -EIO;
 898		/* Keep track of which pipes are transporting */
 899		chip->active_mask &= ~channel_mask;
 900		chip->comm_page->cmd_stop = 0;
 901		chip->comm_page->cmd_reset = 0;
 902		return 0;
 903	}
 904
 905	dev_dbg(chip->card->dev, "pause_transport: No pipes to stop!\n");
 906	return 0;
 907}
 908
 909
 910
 911static int stop_transport(struct echoaudio *chip, u32 channel_mask)
 912{
 913
 914	if (wait_handshake(chip))
 915		return -EIO;
 916
 917	chip->comm_page->cmd_stop |= cpu_to_le32(channel_mask);
 918	chip->comm_page->cmd_reset |= cpu_to_le32(channel_mask);
 919	if (chip->comm_page->cmd_reset) {
 920		clear_handshake(chip);
 921		send_vector(chip, DSP_VC_STOP_TRANSFER);
 922		if (wait_handshake(chip))
 923			return -EIO;
 924		/* Keep track of which pipes are transporting */
 925		chip->active_mask &= ~channel_mask;
 926		chip->comm_page->cmd_stop = 0;
 927		chip->comm_page->cmd_reset = 0;
 928		return 0;
 929	}
 930
 931	dev_dbg(chip->card->dev, "stop_transport: No pipes to stop!\n");
 932	return 0;
 933}
 934
 935
 936
 937static inline int is_pipe_allocated(struct echoaudio *chip, u16 pipe_index)
 938{
 939	return (chip->pipe_alloc_mask & (1 << pipe_index));
 940}
 941
 942
 943
 944/* Stops everything and turns off the DSP. All pipes should be already
 945stopped and unallocated. */
 946static int rest_in_peace(struct echoaudio *chip)
 947{
 948
 949	/* Stops all active pipes (just to be sure) */
 950	stop_transport(chip, chip->active_mask);
 951
 952	set_meters_on(chip, false);
 953
 954#ifdef ECHOCARD_HAS_MIDI
 955	enable_midi_input(chip, false);
 956#endif
 957
 958	/* Go to sleep */
 959	if (chip->dsp_code) {
 960		/* Make load_firmware do a complete reload */
 961		chip->dsp_code = NULL;
 962		/* Put the DSP to sleep */
 963		return send_vector(chip, DSP_VC_GO_COMATOSE);
 964	}
 965	return 0;
 966}
 967
 968
 969
 970/* Fills the comm page with default values */
 971static int init_dsp_comm_page(struct echoaudio *chip)
 972{
 973	/* Check if the compiler added extra padding inside the structure */
 974	if (offsetof(struct comm_page, midi_output) != 0xbe0) {
 975		dev_err(chip->card->dev,
 976			"init_dsp_comm_page() - Invalid struct comm_page structure\n");
 977		return -EPERM;
 978	}
 979
 980	/* Init all the basic stuff */
 981	chip->card_name = ECHOCARD_NAME;
 982	chip->bad_board = true;	/* Set true until DSP loaded */
 983	chip->dsp_code = NULL;	/* Current DSP code not loaded */
 984	chip->asic_loaded = false;
 985	memset(chip->comm_page, 0, sizeof(struct comm_page));
 986
 987	/* Init the comm page */
 988	chip->comm_page->comm_size =
 989		cpu_to_le32(sizeof(struct comm_page));
 990	chip->comm_page->handshake = cpu_to_le32(0xffffffff);
 991	chip->comm_page->midi_out_free_count =
 992		cpu_to_le32(DSP_MIDI_OUT_FIFO_SIZE);
 993	chip->comm_page->sample_rate = cpu_to_le32(44100);
 994
 995	/* Set line levels so we don't blast any inputs on startup */
 996	memset(chip->comm_page->monitors, ECHOGAIN_MUTED, MONITOR_ARRAY_SIZE);
 997	memset(chip->comm_page->vmixer, ECHOGAIN_MUTED, VMIXER_ARRAY_SIZE);
 998
 999	return 0;
1000}
1001
1002
1003
1004/* This function initializes the chip structure with default values, ie. all
1005 * muted and internal clock source. Then it copies the settings to the DSP.
1006 * This MUST be called after the DSP is up and running !
1007 */
1008static int init_line_levels(struct echoaudio *chip)
1009{
1010	memset(chip->output_gain, ECHOGAIN_MUTED, sizeof(chip->output_gain));
1011	memset(chip->input_gain, ECHOGAIN_MUTED, sizeof(chip->input_gain));
1012	memset(chip->monitor_gain, ECHOGAIN_MUTED, sizeof(chip->monitor_gain));
1013	memset(chip->vmixer_gain, ECHOGAIN_MUTED, sizeof(chip->vmixer_gain));
1014	chip->input_clock = ECHO_CLOCK_INTERNAL;
1015	chip->output_clock = ECHO_CLOCK_WORD;
1016	chip->sample_rate = 44100;
1017	return restore_dsp_rettings(chip);
1018}
1019
1020
1021
1022/* This is low level part of the interrupt handler.
1023It returns -1 if the IRQ is not ours, or N>=0 if it is, where N is the number
1024of midi data in the input queue. */
1025static int service_irq(struct echoaudio *chip)
1026{
1027	int st;
1028
1029	/* Read the DSP status register and see if this DSP generated this interrupt */
1030	if (get_dsp_register(chip, CHI32_STATUS_REG) & CHI32_STATUS_IRQ) {
1031		st = 0;
1032#ifdef ECHOCARD_HAS_MIDI
1033		/* Get and parse midi data if present */
1034		if (chip->comm_page->midi_input[0])	/* The count is at index 0 */
1035			st = midi_service_irq(chip);	/* Returns how many midi bytes we received */
1036#endif
1037		/* Clear the hardware interrupt */
1038		chip->comm_page->midi_input[0] = 0;
1039		send_vector(chip, DSP_VC_ACK_INT);
1040		return st;
1041	}
1042	return -1;
1043}
1044
1045
1046
1047
1048/******************************************************************************
1049	Functions for opening and closing pipes
1050 ******************************************************************************/
1051
1052/* allocate_pipes is used to reserve audio pipes for your exclusive use.
1053The call will fail if some pipes are already allocated. */
1054static int allocate_pipes(struct echoaudio *chip, struct audiopipe *pipe,
1055			  int pipe_index, int interleave)
1056{
1057	int i;
1058	u32 channel_mask;
1059
1060	dev_dbg(chip->card->dev,
1061		"allocate_pipes: ch=%d int=%d\n", pipe_index, interleave);
1062
1063	if (chip->bad_board)
1064		return -EIO;
1065
1066	for (channel_mask = i = 0; i < interleave; i++)
1067		channel_mask |= 1 << (pipe_index + i);
1068	if (chip->pipe_alloc_mask & channel_mask) {
1069		dev_err(chip->card->dev,
1070			"allocate_pipes: channel already open\n");
1071		return -EAGAIN;
1072	}
1073
1074	chip->comm_page->position[pipe_index] = 0;
1075	chip->pipe_alloc_mask |= channel_mask;
1076	/* This driver uses cyclic buffers only */
1077	chip->pipe_cyclic_mask |= channel_mask;
1078	pipe->index = pipe_index;
1079	pipe->interleave = interleave;
1080	pipe->state = PIPE_STATE_STOPPED;
1081
1082	/* The counter register is where the DSP writes the 32 bit DMA
1083	position for a pipe.  The DSP is constantly updating this value as
1084	it moves data. The DMA counter is in units of bytes, not samples. */
1085	pipe->dma_counter = (__le32 *)&chip->comm_page->position[pipe_index];
1086	*pipe->dma_counter = 0;
1087	return pipe_index;
1088}
1089
1090
1091
1092static int free_pipes(struct echoaudio *chip, struct audiopipe *pipe)
1093{
1094	u32 channel_mask;
1095	int i;
1096
1097	if (snd_BUG_ON(!is_pipe_allocated(chip, pipe->index)))
1098		return -EINVAL;
1099	if (snd_BUG_ON(pipe->state != PIPE_STATE_STOPPED))
1100		return -EINVAL;
1101
1102	for (channel_mask = i = 0; i < pipe->interleave; i++)
1103		channel_mask |= 1 << (pipe->index + i);
1104
1105	chip->pipe_alloc_mask &= ~channel_mask;
1106	chip->pipe_cyclic_mask &= ~channel_mask;
1107	return 0;
1108}
1109
1110
1111
1112/******************************************************************************
1113	Functions for managing the scatter-gather list
1114******************************************************************************/
1115
1116static int sglist_init(struct echoaudio *chip, struct audiopipe *pipe)
1117{
1118	pipe->sglist_head = 0;
1119	memset(pipe->sgpage.area, 0, PAGE_SIZE);
1120	chip->comm_page->sglist_addr[pipe->index].addr =
1121		cpu_to_le32(pipe->sgpage.addr);
1122	return 0;
1123}
1124
1125
1126
1127static int sglist_add_mapping(struct echoaudio *chip, struct audiopipe *pipe,
1128				dma_addr_t address, size_t length)
1129{
1130	int head = pipe->sglist_head;
1131	struct sg_entry *list = (struct sg_entry *)pipe->sgpage.area;
1132
1133	if (head < MAX_SGLIST_ENTRIES - 1) {
1134		list[head].addr = cpu_to_le32(address);
1135		list[head].size = cpu_to_le32(length);
1136		pipe->sglist_head++;
1137	} else {
1138		dev_err(chip->card->dev, "SGlist: too many fragments\n");
1139		return -ENOMEM;
1140	}
1141	return 0;
1142}
1143
1144
1145
1146static inline int sglist_add_irq(struct echoaudio *chip, struct audiopipe *pipe)
1147{
1148	return sglist_add_mapping(chip, pipe, 0, 0);
1149}
1150
1151
1152
1153static inline int sglist_wrap(struct echoaudio *chip, struct audiopipe *pipe)
1154{
1155	return sglist_add_mapping(chip, pipe, pipe->sgpage.addr, 0);
1156}
v5.9
   1/****************************************************************************
   2
   3   Copyright Echo Digital Audio Corporation (c) 1998 - 2004
   4   All rights reserved
   5   www.echoaudio.com
   6
   7   This file is part of Echo Digital Audio's generic driver library.
   8
   9   Echo Digital Audio's generic driver library is free software;
  10   you can redistribute it and/or modify it under the terms of
  11   the GNU General Public License as published by the Free Software
  12   Foundation.
  13
  14   This program is distributed in the hope that it will be useful,
  15   but WITHOUT ANY WARRANTY; without even the implied warranty of
  16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17   GNU General Public License for more details.
  18
  19   You should have received a copy of the GNU General Public License
  20   along with this program; if not, write to the Free Software
  21   Foundation, Inc., 59 Temple Place - Suite 330, Boston,
  22   MA  02111-1307, USA.
  23
  24   *************************************************************************
  25
  26 Translation from C++ and adaptation for use in ALSA-Driver
  27 were made by Giuliano Pochini <pochini@shiny.it>
  28
  29****************************************************************************/
  30
  31#if PAGE_SIZE < 4096
  32#error PAGE_SIZE is < 4k
  33#endif
  34
  35static int restore_dsp_rettings(struct echoaudio *chip);
  36
  37
  38/* Some vector commands involve the DSP reading or writing data to and from the
  39comm page; if you send one of these commands to the DSP, it will complete the
  40command and then write a non-zero value to the Handshake field in the
  41comm page.  This function waits for the handshake to show up. */
  42static int wait_handshake(struct echoaudio *chip)
  43{
  44	int i;
  45
  46	/* Wait up to 20ms for the handshake from the DSP */
  47	for (i = 0; i < HANDSHAKE_TIMEOUT; i++) {
  48		/* Look for the handshake value */
  49		barrier();
  50		if (chip->comm_page->handshake) {
  51			return 0;
  52		}
  53		udelay(1);
  54	}
  55
  56	dev_err(chip->card->dev, "wait_handshake(): Timeout waiting for DSP\n");
  57	return -EBUSY;
  58}
  59
  60
  61
  62/* Much of the interaction between the DSP and the driver is done via vector
  63commands; send_vector writes a vector command to the DSP.  Typically, this
  64causes the DSP to read or write fields in the comm page.
  65PCI posting is not required thanks to the handshake logic. */
  66static int send_vector(struct echoaudio *chip, u32 command)
  67{
  68	int i;
  69
  70	wmb();	/* Flush all pending writes before sending the command */
  71
  72	/* Wait up to 100ms for the "vector busy" bit to be off */
  73	for (i = 0; i < VECTOR_BUSY_TIMEOUT; i++) {
  74		if (!(get_dsp_register(chip, CHI32_VECTOR_REG) &
  75		      CHI32_VECTOR_BUSY)) {
  76			set_dsp_register(chip, CHI32_VECTOR_REG, command);
  77			/*if (i)  DE_ACT(("send_vector time: %d\n", i));*/
  78			return 0;
  79		}
  80		udelay(1);
  81	}
  82
  83	dev_err(chip->card->dev, "timeout on send_vector\n");
  84	return -EBUSY;
  85}
  86
  87
  88
  89/* write_dsp writes a 32-bit value to the DSP; this is used almost
  90exclusively for loading the DSP. */
  91static int write_dsp(struct echoaudio *chip, u32 data)
  92{
  93	u32 status, i;
  94
  95	for (i = 0; i < 10000000; i++) {	/* timeout = 10s */
  96		status = get_dsp_register(chip, CHI32_STATUS_REG);
  97		if ((status & CHI32_STATUS_HOST_WRITE_EMPTY) != 0) {
  98			set_dsp_register(chip, CHI32_DATA_REG, data);
  99			wmb();			/* write it immediately */
 100			return 0;
 101		}
 102		udelay(1);
 103		cond_resched();
 104	}
 105
 106	chip->bad_board = true;		/* Set true until DSP re-loaded */
 107	dev_dbg(chip->card->dev, "write_dsp: Set bad_board to true\n");
 108	return -EIO;
 109}
 110
 111
 112
 113/* read_dsp reads a 32-bit value from the DSP; this is used almost
 114exclusively for loading the DSP and checking the status of the ASIC. */
 115static int read_dsp(struct echoaudio *chip, u32 *data)
 116{
 117	u32 status, i;
 118
 119	for (i = 0; i < READ_DSP_TIMEOUT; i++) {
 120		status = get_dsp_register(chip, CHI32_STATUS_REG);
 121		if ((status & CHI32_STATUS_HOST_READ_FULL) != 0) {
 122			*data = get_dsp_register(chip, CHI32_DATA_REG);
 123			return 0;
 124		}
 125		udelay(1);
 126		cond_resched();
 127	}
 128
 129	chip->bad_board = true;		/* Set true until DSP re-loaded */
 130	dev_err(chip->card->dev, "read_dsp: Set bad_board to true\n");
 131	return -EIO;
 132}
 133
 134
 135
 136/****************************************************************************
 137	Firmware loading functions
 138 ****************************************************************************/
 139
 140/* This function is used to read back the serial number from the DSP;
 141this is triggered by the SET_COMMPAGE_ADDR command.
 142Only some early Echogals products have serial numbers in the ROM;
 143the serial number is not used, but you still need to do this as
 144part of the DSP load process. */
 145static int read_sn(struct echoaudio *chip)
 146{
 147	int i;
 148	u32 sn[6];
 149
 150	for (i = 0; i < 5; i++) {
 151		if (read_dsp(chip, &sn[i])) {
 152			dev_err(chip->card->dev,
 153				"Failed to read serial number\n");
 154			return -EIO;
 155		}
 156	}
 157	dev_dbg(chip->card->dev,
 158		"Read serial number %08x %08x %08x %08x %08x\n",
 159		 sn[0], sn[1], sn[2], sn[3], sn[4]);
 160	return 0;
 161}
 162
 163
 164
 165#ifndef ECHOCARD_HAS_ASIC
 166/* This card has no ASIC, just return ok */
 167static inline int check_asic_status(struct echoaudio *chip)
 168{
 169	chip->asic_loaded = true;
 170	return 0;
 171}
 172
 173#endif /* !ECHOCARD_HAS_ASIC */
 174
 175
 176
 177#ifdef ECHOCARD_HAS_ASIC
 178
 179/* Load ASIC code - done after the DSP is loaded */
 180static int load_asic_generic(struct echoaudio *chip, u32 cmd, short asic)
 181{
 182	const struct firmware *fw;
 183	int err;
 184	u32 i, size;
 185	u8 *code;
 186
 187	err = get_firmware(&fw, chip, asic);
 188	if (err < 0) {
 189		dev_warn(chip->card->dev, "Firmware not found !\n");
 190		return err;
 191	}
 192
 193	code = (u8 *)fw->data;
 194	size = fw->size;
 195
 196	/* Send the "Here comes the ASIC" command */
 197	if (write_dsp(chip, cmd) < 0)
 198		goto la_error;
 199
 200	/* Write length of ASIC file in bytes */
 201	if (write_dsp(chip, size) < 0)
 202		goto la_error;
 203
 204	for (i = 0; i < size; i++) {
 205		if (write_dsp(chip, code[i]) < 0)
 206			goto la_error;
 207	}
 208
 209	free_firmware(fw, chip);
 210	return 0;
 211
 212la_error:
 213	dev_err(chip->card->dev, "failed on write_dsp\n");
 214	free_firmware(fw, chip);
 215	return -EIO;
 216}
 217
 218#endif /* ECHOCARD_HAS_ASIC */
 219
 220
 221
 222#ifdef DSP_56361
 223
 224/* Install the resident loader for 56361 DSPs;  The resident loader is on
 225the EPROM on the board for 56301 DSP. The resident loader is a tiny little
 226program that is used to load the real DSP code. */
 227static int install_resident_loader(struct echoaudio *chip)
 228{
 229	u32 address;
 230	int index, words, i;
 231	u16 *code;
 232	u32 status;
 233	const struct firmware *fw;
 234
 235	/* 56361 cards only!  This check is required by the old 56301-based
 236	Mona and Gina24 */
 237	if (chip->device_id != DEVICE_ID_56361)
 238		return 0;
 239
 240	/* Look to see if the resident loader is present.  If the resident
 241	loader is already installed, host flag 5 will be on. */
 242	status = get_dsp_register(chip, CHI32_STATUS_REG);
 243	if (status & CHI32_STATUS_REG_HF5) {
 244		dev_dbg(chip->card->dev,
 245			"Resident loader already installed; status is 0x%x\n",
 246			 status);
 247		return 0;
 248	}
 249
 250	i = get_firmware(&fw, chip, FW_361_LOADER);
 251	if (i < 0) {
 252		dev_warn(chip->card->dev, "Firmware not found !\n");
 253		return i;
 254	}
 255
 256	/* The DSP code is an array of 16 bit words.  The array is divided up
 257	into sections.  The first word of each section is the size in words,
 258	followed by the section type.
 259	Since DSP addresses and data are 24 bits wide, they each take up two
 260	16 bit words in the array.
 261	This is a lot like the other loader loop, but it's not a loop, you
 262	don't write the memory type, and you don't write a zero at the end. */
 263
 264	/* Set DSP format bits for 24 bit mode */
 265	set_dsp_register(chip, CHI32_CONTROL_REG,
 266			 get_dsp_register(chip, CHI32_CONTROL_REG) | 0x900);
 267
 268	code = (u16 *)fw->data;
 269
 270	/* Skip the header section; the first word in the array is the size
 271	of the first section, so the first real section of code is pointed
 272	to by Code[0]. */
 273	index = code[0];
 274
 275	/* Skip the section size, LRS block type, and DSP memory type */
 276	index += 3;
 277
 278	/* Get the number of DSP words to write */
 279	words = code[index++];
 280
 281	/* Get the DSP address for this block; 24 bits, so build from two words */
 282	address = ((u32)code[index] << 16) + code[index + 1];
 283	index += 2;
 284
 285	/* Write the count to the DSP */
 286	if (write_dsp(chip, words)) {
 287		dev_err(chip->card->dev,
 288			"install_resident_loader: Failed to write word count!\n");
 289		goto irl_error;
 290	}
 291	/* Write the DSP address */
 292	if (write_dsp(chip, address)) {
 293		dev_err(chip->card->dev,
 294			"install_resident_loader: Failed to write DSP address!\n");
 295		goto irl_error;
 296	}
 297	/* Write out this block of code to the DSP */
 298	for (i = 0; i < words; i++) {
 299		u32 data;
 300
 301		data = ((u32)code[index] << 16) + code[index + 1];
 302		if (write_dsp(chip, data)) {
 303			dev_err(chip->card->dev,
 304				"install_resident_loader: Failed to write DSP code\n");
 305			goto irl_error;
 306		}
 307		index += 2;
 308	}
 309
 310	/* Wait for flag 5 to come up */
 311	for (i = 0; i < 200; i++) {	/* Timeout is 50us * 200 = 10ms */
 312		udelay(50);
 313		status = get_dsp_register(chip, CHI32_STATUS_REG);
 314		if (status & CHI32_STATUS_REG_HF5)
 315			break;
 316	}
 317
 318	if (i == 200) {
 319		dev_err(chip->card->dev, "Resident loader failed to set HF5\n");
 320		goto irl_error;
 321	}
 322
 323	dev_dbg(chip->card->dev, "Resident loader successfully installed\n");
 324	free_firmware(fw, chip);
 325	return 0;
 326
 327irl_error:
 328	free_firmware(fw, chip);
 329	return -EIO;
 330}
 331
 332#endif /* DSP_56361 */
 333
 334
 335static int load_dsp(struct echoaudio *chip, u16 *code)
 336{
 337	u32 address, data;
 338	int index, words, i;
 339
 340	if (chip->dsp_code == code) {
 341		dev_warn(chip->card->dev, "DSP is already loaded!\n");
 342		return 0;
 343	}
 344	chip->bad_board = true;		/* Set true until DSP loaded */
 345	chip->dsp_code = NULL;		/* Current DSP code not loaded */
 346	chip->asic_loaded = false;	/* Loading the DSP code will reset the ASIC */
 347
 348	dev_dbg(chip->card->dev, "load_dsp: Set bad_board to true\n");
 349
 350	/* If this board requires a resident loader, install it. */
 351#ifdef DSP_56361
 352	if ((i = install_resident_loader(chip)) < 0)
 
 353		return i;
 354#endif
 355
 356	/* Send software reset command */
 357	if (send_vector(chip, DSP_VC_RESET) < 0) {
 358		dev_err(chip->card->dev,
 359			"LoadDsp: send_vector DSP_VC_RESET failed, Critical Failure\n");
 360		return -EIO;
 361	}
 362	/* Delay 10us */
 363	udelay(10);
 364
 365	/* Wait 10ms for HF3 to indicate that software reset is complete */
 366	for (i = 0; i < 1000; i++) {	/* Timeout is 10us * 1000 = 10ms */
 367		if (get_dsp_register(chip, CHI32_STATUS_REG) &
 368		    CHI32_STATUS_REG_HF3)
 369			break;
 370		udelay(10);
 371	}
 372
 373	if (i == 1000) {
 374		dev_err(chip->card->dev,
 375			"load_dsp: Timeout waiting for CHI32_STATUS_REG_HF3\n");
 376		return -EIO;
 377	}
 378
 379	/* Set DSP format bits for 24 bit mode now that soft reset is done */
 380	set_dsp_register(chip, CHI32_CONTROL_REG,
 381			 get_dsp_register(chip, CHI32_CONTROL_REG) | 0x900);
 382
 383	/* Main loader loop */
 384
 385	index = code[0];
 386	for (;;) {
 387		int block_type, mem_type;
 388
 389		/* Total Block Size */
 390		index++;
 391
 392		/* Block Type */
 393		block_type = code[index];
 394		if (block_type == 4)	/* We're finished */
 395			break;
 396
 397		index++;
 398
 399		/* Memory Type  P=0,X=1,Y=2 */
 400		mem_type = code[index++];
 401
 402		/* Block Code Size */
 403		words = code[index++];
 404		if (words == 0)		/* We're finished */
 405			break;
 406
 407		/* Start Address */
 408		address = ((u32)code[index] << 16) + code[index + 1];
 409		index += 2;
 410
 411		if (write_dsp(chip, words) < 0) {
 412			dev_err(chip->card->dev,
 413				"load_dsp: failed to write number of DSP words\n");
 414			return -EIO;
 415		}
 416		if (write_dsp(chip, address) < 0) {
 417			dev_err(chip->card->dev,
 418				"load_dsp: failed to write DSP address\n");
 419			return -EIO;
 420		}
 421		if (write_dsp(chip, mem_type) < 0) {
 422			dev_err(chip->card->dev,
 423				"load_dsp: failed to write DSP memory type\n");
 424			return -EIO;
 425		}
 426		/* Code */
 427		for (i = 0; i < words; i++, index+=2) {
 428			data = ((u32)code[index] << 16) + code[index + 1];
 429			if (write_dsp(chip, data) < 0) {
 430				dev_err(chip->card->dev,
 431					"load_dsp: failed to write DSP data\n");
 432				return -EIO;
 433			}
 434		}
 435	}
 436
 437	if (write_dsp(chip, 0) < 0) {	/* We're done!!! */
 438		dev_err(chip->card->dev,
 439			"load_dsp: Failed to write final zero\n");
 440		return -EIO;
 441	}
 442	udelay(10);
 443
 444	for (i = 0; i < 5000; i++) {	/* Timeout is 100us * 5000 = 500ms */
 445		/* Wait for flag 4 - indicates that the DSP loaded OK */
 446		if (get_dsp_register(chip, CHI32_STATUS_REG) &
 447		    CHI32_STATUS_REG_HF4) {
 448			set_dsp_register(chip, CHI32_CONTROL_REG,
 449					 get_dsp_register(chip, CHI32_CONTROL_REG) & ~0x1b00);
 450
 451			if (write_dsp(chip, DSP_FNC_SET_COMMPAGE_ADDR) < 0) {
 452				dev_err(chip->card->dev,
 453					"load_dsp: Failed to write DSP_FNC_SET_COMMPAGE_ADDR\n");
 454				return -EIO;
 455			}
 456
 457			if (write_dsp(chip, chip->comm_page_phys) < 0) {
 458				dev_err(chip->card->dev,
 459					"load_dsp: Failed to write comm page address\n");
 460				return -EIO;
 461			}
 462
 463			/* Get the serial number via slave mode.
 464			This is triggered by the SET_COMMPAGE_ADDR command.
 465			We don't actually use the serial number but we have to
 466			get it as part of the DSP init voodoo. */
 467			if (read_sn(chip) < 0) {
 468				dev_err(chip->card->dev,
 469					"load_dsp: Failed to read serial number\n");
 470				return -EIO;
 471			}
 472
 473			chip->dsp_code = code;		/* Show which DSP code loaded */
 474			chip->bad_board = false;	/* DSP OK */
 475			return 0;
 476		}
 477		udelay(100);
 478	}
 479
 480	dev_err(chip->card->dev,
 481		"load_dsp: DSP load timed out waiting for HF4\n");
 482	return -EIO;
 483}
 484
 485
 486
 487/* load_firmware takes care of loading the DSP and any ASIC code. */
 488static int load_firmware(struct echoaudio *chip)
 489{
 490	const struct firmware *fw;
 491	int box_type, err;
 492
 493	if (snd_BUG_ON(!chip->comm_page))
 494		return -EPERM;
 495
 496	/* See if the ASIC is present and working - only if the DSP is already loaded */
 497	if (chip->dsp_code) {
 498		if ((box_type = check_asic_status(chip)) >= 0)
 
 499			return box_type;
 500		/* ASIC check failed; force the DSP to reload */
 501		chip->dsp_code = NULL;
 502	}
 503
 504	err = get_firmware(&fw, chip, chip->dsp_code_to_load);
 505	if (err < 0)
 506		return err;
 507	err = load_dsp(chip, (u16 *)fw->data);
 508	free_firmware(fw, chip);
 509	if (err < 0)
 510		return err;
 511
 512	if ((box_type = load_asic(chip)) < 0)
 
 513		return box_type;	/* error */
 514
 515	return box_type;
 516}
 517
 518
 519
 520/****************************************************************************
 521	Mixer functions
 522 ****************************************************************************/
 523
 524#if defined(ECHOCARD_HAS_INPUT_NOMINAL_LEVEL) || \
 525	defined(ECHOCARD_HAS_OUTPUT_NOMINAL_LEVEL)
 526
 527/* Set the nominal level for an input or output bus (true = -10dBV, false = +4dBu) */
 528static int set_nominal_level(struct echoaudio *chip, u16 index, char consumer)
 529{
 530	if (snd_BUG_ON(index >= num_busses_out(chip) + num_busses_in(chip)))
 531		return -EINVAL;
 532
 533	/* Wait for the handshake (OK even if ASIC is not loaded) */
 534	if (wait_handshake(chip))
 535		return -EIO;
 536
 537	chip->nominal_level[index] = consumer;
 538
 539	if (consumer)
 540		chip->comm_page->nominal_level_mask |= cpu_to_le32(1 << index);
 541	else
 542		chip->comm_page->nominal_level_mask &= ~cpu_to_le32(1 << index);
 543
 544	return 0;
 545}
 546
 547#endif /* ECHOCARD_HAS_*_NOMINAL_LEVEL */
 548
 549
 550
 551/* Set the gain for a single physical output channel (dB). */
 552static int set_output_gain(struct echoaudio *chip, u16 channel, s8 gain)
 553{
 554	if (snd_BUG_ON(channel >= num_busses_out(chip)))
 555		return -EINVAL;
 556
 557	if (wait_handshake(chip))
 558		return -EIO;
 559
 560	/* Save the new value */
 561	chip->output_gain[channel] = gain;
 562	chip->comm_page->line_out_level[channel] = gain;
 563	return 0;
 564}
 565
 566
 567
 568#ifdef ECHOCARD_HAS_MONITOR
 569/* Set the monitor level from an input bus to an output bus. */
 570static int set_monitor_gain(struct echoaudio *chip, u16 output, u16 input,
 571			    s8 gain)
 572{
 573	if (snd_BUG_ON(output >= num_busses_out(chip) ||
 574		    input >= num_busses_in(chip)))
 575		return -EINVAL;
 576
 577	if (wait_handshake(chip))
 578		return -EIO;
 579
 580	chip->monitor_gain[output][input] = gain;
 581	chip->comm_page->monitors[monitor_index(chip, output, input)] = gain;
 582	return 0;
 583}
 584#endif /* ECHOCARD_HAS_MONITOR */
 585
 586
 587/* Tell the DSP to read and update output, nominal & monitor levels in comm page. */
 588static int update_output_line_level(struct echoaudio *chip)
 589{
 590	if (wait_handshake(chip))
 591		return -EIO;
 592	clear_handshake(chip);
 593	return send_vector(chip, DSP_VC_UPDATE_OUTVOL);
 594}
 595
 596
 597
 598/* Tell the DSP to read and update input levels in comm page */
 599static int update_input_line_level(struct echoaudio *chip)
 600{
 601	if (wait_handshake(chip))
 602		return -EIO;
 603	clear_handshake(chip);
 604	return send_vector(chip, DSP_VC_UPDATE_INGAIN);
 605}
 606
 607
 608
 609/* set_meters_on turns the meters on or off.  If meters are turned on, the DSP
 610will write the meter and clock detect values to the comm page at about 30Hz */
 611static void set_meters_on(struct echoaudio *chip, char on)
 612{
 613	if (on && !chip->meters_enabled) {
 614		send_vector(chip, DSP_VC_METERS_ON);
 615		chip->meters_enabled = 1;
 616	} else if (!on && chip->meters_enabled) {
 617		send_vector(chip, DSP_VC_METERS_OFF);
 618		chip->meters_enabled = 0;
 619		memset((s8 *)chip->comm_page->vu_meter, ECHOGAIN_MUTED,
 620		       DSP_MAXPIPES);
 621		memset((s8 *)chip->comm_page->peak_meter, ECHOGAIN_MUTED,
 622		       DSP_MAXPIPES);
 623	}
 624}
 625
 626
 627
 628/* Fill out an the given array using the current values in the comm page.
 629Meters are written in the comm page by the DSP in this order:
 630 Output busses
 631 Input busses
 632 Output pipes (vmixer cards only)
 633
 634This function assumes there are no more than 16 in/out busses or pipes
 635Meters is an array [3][16][2] of long. */
 636static void get_audio_meters(struct echoaudio *chip, long *meters)
 637{
 638	unsigned int i, m, n;
 639
 640	for (i = 0 ; i < 96; i++)
 641		meters[i] = 0;
 642
 643	for (m = 0, n = 0, i = 0; i < num_busses_out(chip); i++, m++) {
 644		meters[n++] = chip->comm_page->vu_meter[m];
 645		meters[n++] = chip->comm_page->peak_meter[m];
 646	}
 647
 648#ifdef ECHOCARD_ECHO3G
 649	m = E3G_MAX_OUTPUTS;	/* Skip unused meters */
 650#endif
 651
 652	for (n = 32, i = 0; i < num_busses_in(chip); i++, m++) {
 653		meters[n++] = chip->comm_page->vu_meter[m];
 654		meters[n++] = chip->comm_page->peak_meter[m];
 655	}
 656#ifdef ECHOCARD_HAS_VMIXER
 657	for (n = 64, i = 0; i < num_pipes_out(chip); i++, m++) {
 658		meters[n++] = chip->comm_page->vu_meter[m];
 659		meters[n++] = chip->comm_page->peak_meter[m];
 660	}
 661#endif
 662}
 663
 664
 665
 666static int restore_dsp_rettings(struct echoaudio *chip)
 667{
 668	int i, o, err;
 669
 670	if ((err = check_asic_status(chip)) < 0)
 
 671		return err;
 672
 673	/* Gina20/Darla20 only. Should be harmless for other cards. */
 674	chip->comm_page->gd_clock_state = GD_CLOCK_UNDEF;
 675	chip->comm_page->gd_spdif_status = GD_SPDIF_STATUS_UNDEF;
 676	chip->comm_page->handshake = cpu_to_le32(0xffffffff);
 677
 678	/* Restore output busses */
 679	for (i = 0; i < num_busses_out(chip); i++) {
 680		err = set_output_gain(chip, i, chip->output_gain[i]);
 681		if (err < 0)
 682			return err;
 683	}
 684
 685#ifdef ECHOCARD_HAS_VMIXER
 686	for (i = 0; i < num_pipes_out(chip); i++)
 687		for (o = 0; o < num_busses_out(chip); o++) {
 688			err = set_vmixer_gain(chip, o, i,
 689						chip->vmixer_gain[o][i]);
 690			if (err < 0)
 691				return err;
 692		}
 693	if (update_vmixer_level(chip) < 0)
 694		return -EIO;
 695#endif /* ECHOCARD_HAS_VMIXER */
 696
 697#ifdef ECHOCARD_HAS_MONITOR
 698	for (o = 0; o < num_busses_out(chip); o++)
 699		for (i = 0; i < num_busses_in(chip); i++) {
 700			err = set_monitor_gain(chip, o, i,
 701						chip->monitor_gain[o][i]);
 702			if (err < 0)
 703				return err;
 704		}
 705#endif /* ECHOCARD_HAS_MONITOR */
 706
 707#ifdef ECHOCARD_HAS_INPUT_GAIN
 708	for (i = 0; i < num_busses_in(chip); i++) {
 709		err = set_input_gain(chip, i, chip->input_gain[i]);
 710		if (err < 0)
 711			return err;
 712	}
 713#endif /* ECHOCARD_HAS_INPUT_GAIN */
 714
 715	err = update_output_line_level(chip);
 716	if (err < 0)
 717		return err;
 718
 719	err = update_input_line_level(chip);
 720	if (err < 0)
 721		return err;
 722
 723	err = set_sample_rate(chip, chip->sample_rate);
 724	if (err < 0)
 725		return err;
 726
 727	if (chip->meters_enabled) {
 728		err = send_vector(chip, DSP_VC_METERS_ON);
 729		if (err < 0)
 730			return err;
 731	}
 732
 733#ifdef ECHOCARD_HAS_DIGITAL_MODE_SWITCH
 734	if (set_digital_mode(chip, chip->digital_mode) < 0)
 735		return -EIO;
 736#endif
 737
 738#ifdef ECHOCARD_HAS_DIGITAL_IO
 739	if (set_professional_spdif(chip, chip->professional_spdif) < 0)
 740		return -EIO;
 741#endif
 742
 743#ifdef ECHOCARD_HAS_PHANTOM_POWER
 744	if (set_phantom_power(chip, chip->phantom_power) < 0)
 745		return -EIO;
 746#endif
 747
 748#ifdef ECHOCARD_HAS_EXTERNAL_CLOCK
 749	/* set_input_clock() also restores automute setting */
 750	if (set_input_clock(chip, chip->input_clock) < 0)
 751		return -EIO;
 752#endif
 753
 754#ifdef ECHOCARD_HAS_OUTPUT_CLOCK_SWITCH
 755	if (set_output_clock(chip, chip->output_clock) < 0)
 756		return -EIO;
 757#endif
 758
 759	if (wait_handshake(chip) < 0)
 760		return -EIO;
 761	clear_handshake(chip);
 762	if (send_vector(chip, DSP_VC_UPDATE_FLAGS) < 0)
 763		return -EIO;
 764
 765	return 0;
 766}
 767
 768
 769
 770/****************************************************************************
 771	Transport functions
 772 ****************************************************************************/
 773
 774/* set_audio_format() sets the format of the audio data in host memory for
 775this pipe.  Note that _MS_ (mono-to-stereo) playback modes are not used by ALSA
 776but they are here because they are just mono while capturing */
 777static void set_audio_format(struct echoaudio *chip, u16 pipe_index,
 778			     const struct audioformat *format)
 779{
 780	u16 dsp_format;
 781
 782	dsp_format = DSP_AUDIOFORM_SS_16LE;
 783
 784	/* Look for super-interleave (no big-endian and 8 bits) */
 785	if (format->interleave > 2) {
 786		switch (format->bits_per_sample) {
 787		case 16:
 788			dsp_format = DSP_AUDIOFORM_SUPER_INTERLEAVE_16LE;
 789			break;
 790		case 24:
 791			dsp_format = DSP_AUDIOFORM_SUPER_INTERLEAVE_24LE;
 792			break;
 793		case 32:
 794			dsp_format = DSP_AUDIOFORM_SUPER_INTERLEAVE_32LE;
 795			break;
 796		}
 797		dsp_format |= format->interleave;
 798	} else if (format->data_are_bigendian) {
 799		/* For big-endian data, only 32 bit samples are supported */
 800		switch (format->interleave) {
 801		case 1:
 802			dsp_format = DSP_AUDIOFORM_MM_32BE;
 803			break;
 804#ifdef ECHOCARD_HAS_STEREO_BIG_ENDIAN32
 805		case 2:
 806			dsp_format = DSP_AUDIOFORM_SS_32BE;
 807			break;
 808#endif
 809		}
 810	} else if (format->interleave == 1 &&
 811		   format->bits_per_sample == 32 && !format->mono_to_stereo) {
 812		/* 32 bit little-endian mono->mono case */
 813		dsp_format = DSP_AUDIOFORM_MM_32LE;
 814	} else {
 815		/* Handle the other little-endian formats */
 816		switch (format->bits_per_sample) {
 817		case 8:
 818			if (format->interleave == 2)
 819				dsp_format = DSP_AUDIOFORM_SS_8;
 820			else
 821				dsp_format = DSP_AUDIOFORM_MS_8;
 822			break;
 823		default:
 824		case 16:
 825			if (format->interleave == 2)
 826				dsp_format = DSP_AUDIOFORM_SS_16LE;
 827			else
 828				dsp_format = DSP_AUDIOFORM_MS_16LE;
 829			break;
 830		case 24:
 831			if (format->interleave == 2)
 832				dsp_format = DSP_AUDIOFORM_SS_24LE;
 833			else
 834				dsp_format = DSP_AUDIOFORM_MS_24LE;
 835			break;
 836		case 32:
 837			if (format->interleave == 2)
 838				dsp_format = DSP_AUDIOFORM_SS_32LE;
 839			else
 840				dsp_format = DSP_AUDIOFORM_MS_32LE;
 841			break;
 842		}
 843	}
 844	dev_dbg(chip->card->dev,
 845		 "set_audio_format[%d] = %x\n", pipe_index, dsp_format);
 846	chip->comm_page->audio_format[pipe_index] = cpu_to_le16(dsp_format);
 847}
 848
 849
 850
 851/* start_transport starts transport for a set of pipes.
 852The bits 1 in channel_mask specify what pipes to start. Only the bit of the
 853first channel must be set, regardless its interleave.
 854Same thing for pause_ and stop_ -trasport below. */
 855static int start_transport(struct echoaudio *chip, u32 channel_mask,
 856			   u32 cyclic_mask)
 857{
 858
 859	if (wait_handshake(chip))
 860		return -EIO;
 861
 862	chip->comm_page->cmd_start |= cpu_to_le32(channel_mask);
 863
 864	if (chip->comm_page->cmd_start) {
 865		clear_handshake(chip);
 866		send_vector(chip, DSP_VC_START_TRANSFER);
 867		if (wait_handshake(chip))
 868			return -EIO;
 869		/* Keep track of which pipes are transporting */
 870		chip->active_mask |= channel_mask;
 871		chip->comm_page->cmd_start = 0;
 872		return 0;
 873	}
 874
 875	dev_err(chip->card->dev, "start_transport: No pipes to start!\n");
 876	return -EINVAL;
 877}
 878
 879
 880
 881static int pause_transport(struct echoaudio *chip, u32 channel_mask)
 882{
 883
 884	if (wait_handshake(chip))
 885		return -EIO;
 886
 887	chip->comm_page->cmd_stop |= cpu_to_le32(channel_mask);
 888	chip->comm_page->cmd_reset = 0;
 889	if (chip->comm_page->cmd_stop) {
 890		clear_handshake(chip);
 891		send_vector(chip, DSP_VC_STOP_TRANSFER);
 892		if (wait_handshake(chip))
 893			return -EIO;
 894		/* Keep track of which pipes are transporting */
 895		chip->active_mask &= ~channel_mask;
 896		chip->comm_page->cmd_stop = 0;
 897		chip->comm_page->cmd_reset = 0;
 898		return 0;
 899	}
 900
 901	dev_dbg(chip->card->dev, "pause_transport: No pipes to stop!\n");
 902	return 0;
 903}
 904
 905
 906
 907static int stop_transport(struct echoaudio *chip, u32 channel_mask)
 908{
 909
 910	if (wait_handshake(chip))
 911		return -EIO;
 912
 913	chip->comm_page->cmd_stop |= cpu_to_le32(channel_mask);
 914	chip->comm_page->cmd_reset |= cpu_to_le32(channel_mask);
 915	if (chip->comm_page->cmd_reset) {
 916		clear_handshake(chip);
 917		send_vector(chip, DSP_VC_STOP_TRANSFER);
 918		if (wait_handshake(chip))
 919			return -EIO;
 920		/* Keep track of which pipes are transporting */
 921		chip->active_mask &= ~channel_mask;
 922		chip->comm_page->cmd_stop = 0;
 923		chip->comm_page->cmd_reset = 0;
 924		return 0;
 925	}
 926
 927	dev_dbg(chip->card->dev, "stop_transport: No pipes to stop!\n");
 928	return 0;
 929}
 930
 931
 932
 933static inline int is_pipe_allocated(struct echoaudio *chip, u16 pipe_index)
 934{
 935	return (chip->pipe_alloc_mask & (1 << pipe_index));
 936}
 937
 938
 939
 940/* Stops everything and turns off the DSP. All pipes should be already
 941stopped and unallocated. */
 942static int rest_in_peace(struct echoaudio *chip)
 943{
 944
 945	/* Stops all active pipes (just to be sure) */
 946	stop_transport(chip, chip->active_mask);
 947
 948	set_meters_on(chip, false);
 949
 950#ifdef ECHOCARD_HAS_MIDI
 951	enable_midi_input(chip, false);
 952#endif
 953
 954	/* Go to sleep */
 955	if (chip->dsp_code) {
 956		/* Make load_firmware do a complete reload */
 957		chip->dsp_code = NULL;
 958		/* Put the DSP to sleep */
 959		return send_vector(chip, DSP_VC_GO_COMATOSE);
 960	}
 961	return 0;
 962}
 963
 964
 965
 966/* Fills the comm page with default values */
 967static int init_dsp_comm_page(struct echoaudio *chip)
 968{
 969	/* Check if the compiler added extra padding inside the structure */
 970	if (offsetof(struct comm_page, midi_output) != 0xbe0) {
 971		dev_err(chip->card->dev,
 972			"init_dsp_comm_page() - Invalid struct comm_page structure\n");
 973		return -EPERM;
 974	}
 975
 976	/* Init all the basic stuff */
 977	chip->card_name = ECHOCARD_NAME;
 978	chip->bad_board = true;	/* Set true until DSP loaded */
 979	chip->dsp_code = NULL;	/* Current DSP code not loaded */
 980	chip->asic_loaded = false;
 981	memset(chip->comm_page, 0, sizeof(struct comm_page));
 982
 983	/* Init the comm page */
 984	chip->comm_page->comm_size =
 985		cpu_to_le32(sizeof(struct comm_page));
 986	chip->comm_page->handshake = cpu_to_le32(0xffffffff);
 987	chip->comm_page->midi_out_free_count =
 988		cpu_to_le32(DSP_MIDI_OUT_FIFO_SIZE);
 989	chip->comm_page->sample_rate = cpu_to_le32(44100);
 990
 991	/* Set line levels so we don't blast any inputs on startup */
 992	memset(chip->comm_page->monitors, ECHOGAIN_MUTED, MONITOR_ARRAY_SIZE);
 993	memset(chip->comm_page->vmixer, ECHOGAIN_MUTED, VMIXER_ARRAY_SIZE);
 994
 995	return 0;
 996}
 997
 998
 999
1000/* This function initializes the chip structure with default values, ie. all
1001 * muted and internal clock source. Then it copies the settings to the DSP.
1002 * This MUST be called after the DSP is up and running !
1003 */
1004static int init_line_levels(struct echoaudio *chip)
1005{
1006	memset(chip->output_gain, ECHOGAIN_MUTED, sizeof(chip->output_gain));
1007	memset(chip->input_gain, ECHOGAIN_MUTED, sizeof(chip->input_gain));
1008	memset(chip->monitor_gain, ECHOGAIN_MUTED, sizeof(chip->monitor_gain));
1009	memset(chip->vmixer_gain, ECHOGAIN_MUTED, sizeof(chip->vmixer_gain));
1010	chip->input_clock = ECHO_CLOCK_INTERNAL;
1011	chip->output_clock = ECHO_CLOCK_WORD;
1012	chip->sample_rate = 44100;
1013	return restore_dsp_rettings(chip);
1014}
1015
1016
1017
1018/* This is low level part of the interrupt handler.
1019It returns -1 if the IRQ is not ours, or N>=0 if it is, where N is the number
1020of midi data in the input queue. */
1021static int service_irq(struct echoaudio *chip)
1022{
1023	int st;
1024
1025	/* Read the DSP status register and see if this DSP generated this interrupt */
1026	if (get_dsp_register(chip, CHI32_STATUS_REG) & CHI32_STATUS_IRQ) {
1027		st = 0;
1028#ifdef ECHOCARD_HAS_MIDI
1029		/* Get and parse midi data if present */
1030		if (chip->comm_page->midi_input[0])	/* The count is at index 0 */
1031			st = midi_service_irq(chip);	/* Returns how many midi bytes we received */
1032#endif
1033		/* Clear the hardware interrupt */
1034		chip->comm_page->midi_input[0] = 0;
1035		send_vector(chip, DSP_VC_ACK_INT);
1036		return st;
1037	}
1038	return -1;
1039}
1040
1041
1042
1043
1044/******************************************************************************
1045	Functions for opening and closing pipes
1046 ******************************************************************************/
1047
1048/* allocate_pipes is used to reserve audio pipes for your exclusive use.
1049The call will fail if some pipes are already allocated. */
1050static int allocate_pipes(struct echoaudio *chip, struct audiopipe *pipe,
1051			  int pipe_index, int interleave)
1052{
1053	int i;
1054	u32 channel_mask;
1055
1056	dev_dbg(chip->card->dev,
1057		"allocate_pipes: ch=%d int=%d\n", pipe_index, interleave);
1058
1059	if (chip->bad_board)
1060		return -EIO;
1061
1062	for (channel_mask = i = 0; i < interleave; i++)
1063		channel_mask |= 1 << (pipe_index + i);
1064	if (chip->pipe_alloc_mask & channel_mask) {
1065		dev_err(chip->card->dev,
1066			"allocate_pipes: channel already open\n");
1067		return -EAGAIN;
1068	}
1069
1070	chip->comm_page->position[pipe_index] = 0;
1071	chip->pipe_alloc_mask |= channel_mask;
1072	/* This driver uses cyclic buffers only */
1073	chip->pipe_cyclic_mask |= channel_mask;
1074	pipe->index = pipe_index;
1075	pipe->interleave = interleave;
1076	pipe->state = PIPE_STATE_STOPPED;
1077
1078	/* The counter register is where the DSP writes the 32 bit DMA
1079	position for a pipe.  The DSP is constantly updating this value as
1080	it moves data. The DMA counter is in units of bytes, not samples. */
1081	pipe->dma_counter = (__le32 *)&chip->comm_page->position[pipe_index];
1082	*pipe->dma_counter = 0;
1083	return pipe_index;
1084}
1085
1086
1087
1088static int free_pipes(struct echoaudio *chip, struct audiopipe *pipe)
1089{
1090	u32 channel_mask;
1091	int i;
1092
1093	if (snd_BUG_ON(!is_pipe_allocated(chip, pipe->index)))
1094		return -EINVAL;
1095	if (snd_BUG_ON(pipe->state != PIPE_STATE_STOPPED))
1096		return -EINVAL;
1097
1098	for (channel_mask = i = 0; i < pipe->interleave; i++)
1099		channel_mask |= 1 << (pipe->index + i);
1100
1101	chip->pipe_alloc_mask &= ~channel_mask;
1102	chip->pipe_cyclic_mask &= ~channel_mask;
1103	return 0;
1104}
1105
1106
1107
1108/******************************************************************************
1109	Functions for managing the scatter-gather list
1110******************************************************************************/
1111
1112static int sglist_init(struct echoaudio *chip, struct audiopipe *pipe)
1113{
1114	pipe->sglist_head = 0;
1115	memset(pipe->sgpage.area, 0, PAGE_SIZE);
1116	chip->comm_page->sglist_addr[pipe->index].addr =
1117		cpu_to_le32(pipe->sgpage.addr);
1118	return 0;
1119}
1120
1121
1122
1123static int sglist_add_mapping(struct echoaudio *chip, struct audiopipe *pipe,
1124				dma_addr_t address, size_t length)
1125{
1126	int head = pipe->sglist_head;
1127	struct sg_entry *list = (struct sg_entry *)pipe->sgpage.area;
1128
1129	if (head < MAX_SGLIST_ENTRIES - 1) {
1130		list[head].addr = cpu_to_le32(address);
1131		list[head].size = cpu_to_le32(length);
1132		pipe->sglist_head++;
1133	} else {
1134		dev_err(chip->card->dev, "SGlist: too many fragments\n");
1135		return -ENOMEM;
1136	}
1137	return 0;
1138}
1139
1140
1141
1142static inline int sglist_add_irq(struct echoaudio *chip, struct audiopipe *pipe)
1143{
1144	return sglist_add_mapping(chip, pipe, 0, 0);
1145}
1146
1147
1148
1149static inline int sglist_wrap(struct echoaudio *chip, struct audiopipe *pipe)
1150{
1151	return sglist_add_mapping(chip, pipe, pipe->sgpage.addr, 0);
1152}