Linux Audio

Check our new training course

Loading...
v4.10.11
 
   1/*
   2 *   This program is free software; you can redistribute it and/or modify
   3 *   it under the terms of the GNU General Public License as published by
   4 *   the Free Software Foundation; either version 2 of the License, or
   5 *   (at your option) any later version.
   6 *
   7 *   This program is distributed in the hope that it will be useful,
   8 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
   9 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  10 *   GNU General Public License for more details.
  11 *
  12 *   You should have received a copy of the GNU General Public License
  13 *   along with this program; if not, write to the Free Software
  14 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
  15 *
  16 */
  17
  18/*
  19 * 2002-07 Benny Sjostrand benny@hostmobility.com
  20 */
  21
  22
  23#include <linux/io.h>
  24#include <linux/delay.h>
  25#include <linux/pm.h>
  26#include <linux/init.h>
  27#include <linux/slab.h>
  28#include <linux/vmalloc.h>
  29#include <linux/mutex.h>
  30
  31#include <sound/core.h>
  32#include <sound/control.h>
  33#include <sound/info.h>
  34#include <sound/asoundef.h>
  35#include "cs46xx.h"
  36
  37#include "cs46xx_lib.h"
  38#include "dsp_spos.h"
  39
  40static int cs46xx_dsp_async_init (struct snd_cs46xx *chip,
  41				  struct dsp_scb_descriptor * fg_entry);
  42
  43static enum wide_opcode wide_opcodes[] = { 
  44	WIDE_FOR_BEGIN_LOOP,
  45	WIDE_FOR_BEGIN_LOOP2,
  46	WIDE_COND_GOTO_ADDR,
  47	WIDE_COND_GOTO_CALL,
  48	WIDE_TBEQ_COND_GOTO_ADDR,
  49	WIDE_TBEQ_COND_CALL_ADDR,
  50	WIDE_TBEQ_NCOND_GOTO_ADDR,
  51	WIDE_TBEQ_NCOND_CALL_ADDR,
  52	WIDE_TBEQ_COND_GOTO1_ADDR,
  53	WIDE_TBEQ_COND_CALL1_ADDR,
  54	WIDE_TBEQ_NCOND_GOTOI_ADDR,
  55	WIDE_TBEQ_NCOND_CALL1_ADDR
  56};
  57
  58static int shadow_and_reallocate_code (struct snd_cs46xx * chip, u32 * data, u32 size,
  59				       u32 overlay_begin_address)
  60{
  61	unsigned int i = 0, j, nreallocated = 0;
  62	u32 hival,loval,address;
  63	u32 mop_operands,mop_type,wide_op;
  64	struct dsp_spos_instance * ins = chip->dsp_spos_instance;
  65
  66	if (snd_BUG_ON(size %2))
  67		return -EINVAL;
  68  
  69	while (i < size) {
  70		loval = data[i++];
  71		hival = data[i++];
  72
  73		if (ins->code.offset > 0) {
  74			mop_operands = (hival >> 6) & 0x03fff;
  75			mop_type = mop_operands >> 10;
  76      
  77			/* check for wide type instruction */
  78			if (mop_type == 0 &&
  79			    (mop_operands & WIDE_LADD_INSTR_MASK) == 0 &&
  80			    (mop_operands & WIDE_INSTR_MASK) != 0) {
  81				wide_op = loval & 0x7f;
  82				for (j = 0;j < ARRAY_SIZE(wide_opcodes); ++j) {
  83					if (wide_opcodes[j] == wide_op) {
  84						/* need to reallocate instruction */
  85						address  = (hival & 0x00FFF) << 5;
  86						address |=  loval >> 15;
  87            
  88						dev_dbg(chip->card->dev,
  89							"handle_wideop[1]: %05x:%05x addr %04x\n",
  90							hival, loval, address);
  91            
  92						if ( !(address & 0x8000) ) {
  93							address += (ins->code.offset / 2) - overlay_begin_address;
  94						} else {
  95							dev_dbg(chip->card->dev,
  96								"handle_wideop[1]: ROM symbol not reallocated\n");
  97						}
  98            
  99						hival &= 0xFF000;
 100						loval &= 0x07FFF;
 101            
 102						hival |= ( (address >> 5)  & 0x00FFF);
 103						loval |= ( (address << 15) & 0xF8000);
 104            
 105						address  = (hival & 0x00FFF) << 5;
 106						address |=  loval >> 15;
 107            
 108						dev_dbg(chip->card->dev,
 109							"handle_wideop:[2] %05x:%05x addr %04x\n",
 110							hival, loval, address);
 111						nreallocated++;
 112					} /* wide_opcodes[j] == wide_op */
 113				} /* for */
 114			} /* mod_type == 0 ... */
 115		} /* ins->code.offset > 0 */
 116
 117		ins->code.data[ins->code.size++] = loval;
 118		ins->code.data[ins->code.size++] = hival;
 119	}
 120
 121	dev_dbg(chip->card->dev,
 122		"dsp_spos: %d instructions reallocated\n", nreallocated);
 123	return nreallocated;
 124}
 125
 126static struct dsp_segment_desc * get_segment_desc (struct dsp_module_desc * module, int seg_type)
 127{
 128	int i;
 129	for (i = 0;i < module->nsegments; ++i) {
 130		if (module->segments[i].segment_type == seg_type) {
 131			return (module->segments + i);
 132		}
 133	}
 134
 135	return NULL;
 136};
 137
 138static int find_free_symbol_index (struct dsp_spos_instance * ins)
 139{
 140	int index = ins->symbol_table.nsymbols,i;
 141
 142	for (i = ins->symbol_table.highest_frag_index; i < ins->symbol_table.nsymbols; ++i) {
 143		if (ins->symbol_table.symbols[i].deleted) {
 144			index = i;
 145			break;
 146		}
 147	}
 148
 149	return index;
 150}
 151
 152static int add_symbols (struct snd_cs46xx * chip, struct dsp_module_desc * module)
 153{
 154	int i;
 155	struct dsp_spos_instance * ins = chip->dsp_spos_instance;
 156
 157	if (module->symbol_table.nsymbols > 0) {
 158		if (!strcmp(module->symbol_table.symbols[0].symbol_name, "OVERLAYBEGINADDRESS") &&
 159		    module->symbol_table.symbols[0].symbol_type == SYMBOL_CONSTANT ) {
 160			module->overlay_begin_address = module->symbol_table.symbols[0].address;
 161		}
 162	}
 163
 164	for (i = 0;i < module->symbol_table.nsymbols; ++i) {
 165		if (ins->symbol_table.nsymbols == (DSP_MAX_SYMBOLS - 1)) {
 166			dev_err(chip->card->dev,
 167				"dsp_spos: symbol table is full\n");
 168			return -ENOMEM;
 169		}
 170
 171
 172		if (cs46xx_dsp_lookup_symbol(chip,
 173					     module->symbol_table.symbols[i].symbol_name,
 174					     module->symbol_table.symbols[i].symbol_type) == NULL) {
 175
 176			ins->symbol_table.symbols[ins->symbol_table.nsymbols] = module->symbol_table.symbols[i];
 177			ins->symbol_table.symbols[ins->symbol_table.nsymbols].address += ((ins->code.offset / 2) - module->overlay_begin_address);
 178			ins->symbol_table.symbols[ins->symbol_table.nsymbols].module = module;
 179			ins->symbol_table.symbols[ins->symbol_table.nsymbols].deleted = 0;
 180
 181			if (ins->symbol_table.nsymbols > ins->symbol_table.highest_frag_index) 
 182				ins->symbol_table.highest_frag_index = ins->symbol_table.nsymbols;
 183
 184			ins->symbol_table.nsymbols++;
 185		} else {
 186#if 0
 187			dev_dbg(chip->card->dev,
 188				"dsp_spos: symbol <%s> duplicated, probably nothing wrong with that (Cirrus?)\n",
 189				module->symbol_table.symbols[i].symbol_name); */
 190#endif
 191		}
 192	}
 193
 194	return 0;
 195}
 196
 197static struct dsp_symbol_entry *
 198add_symbol (struct snd_cs46xx * chip, char * symbol_name, u32 address, int type)
 199{
 200	struct dsp_spos_instance * ins = chip->dsp_spos_instance;
 201	struct dsp_symbol_entry * symbol = NULL;
 202	int index;
 203
 204	if (ins->symbol_table.nsymbols == (DSP_MAX_SYMBOLS - 1)) {
 205		dev_err(chip->card->dev, "dsp_spos: symbol table is full\n");
 206		return NULL;
 207	}
 208  
 209	if (cs46xx_dsp_lookup_symbol(chip,
 210				     symbol_name,
 211				     type) != NULL) {
 212		dev_err(chip->card->dev,
 213			"dsp_spos: symbol <%s> duplicated\n", symbol_name);
 214		return NULL;
 215	}
 216
 217	index = find_free_symbol_index (ins);
 218
 219	strcpy (ins->symbol_table.symbols[index].symbol_name, symbol_name);
 220	ins->symbol_table.symbols[index].address = address;
 221	ins->symbol_table.symbols[index].symbol_type = type;
 222	ins->symbol_table.symbols[index].module = NULL;
 223	ins->symbol_table.symbols[index].deleted = 0;
 224	symbol = (ins->symbol_table.symbols + index);
 225
 226	if (index > ins->symbol_table.highest_frag_index) 
 227		ins->symbol_table.highest_frag_index = index;
 228
 229	if (index == ins->symbol_table.nsymbols)
 230		ins->symbol_table.nsymbols++; /* no frag. in list */
 231
 232	return symbol;
 233}
 234
 235struct dsp_spos_instance *cs46xx_dsp_spos_create (struct snd_cs46xx * chip)
 236{
 237	struct dsp_spos_instance * ins = kzalloc(sizeof(struct dsp_spos_instance), GFP_KERNEL);
 238
 239	if (ins == NULL)
 240		return NULL;
 241
 242	/* better to use vmalloc for this big table */
 243	ins->symbol_table.symbols = vmalloc(sizeof(struct dsp_symbol_entry) *
 244					    DSP_MAX_SYMBOLS);
 
 245	ins->code.data = kmalloc(DSP_CODE_BYTE_SIZE, GFP_KERNEL);
 246	ins->modules = kmalloc(sizeof(struct dsp_module_desc) * DSP_MAX_MODULES, GFP_KERNEL);
 
 
 247	if (!ins->symbol_table.symbols || !ins->code.data || !ins->modules) {
 248		cs46xx_dsp_spos_destroy(chip);
 249		goto error;
 250	}
 251	ins->symbol_table.nsymbols = 0;
 252	ins->symbol_table.highest_frag_index = 0;
 253	ins->code.offset = 0;
 254	ins->code.size = 0;
 255	ins->nscb = 0;
 256	ins->ntask = 0;
 257	ins->nmodules = 0;
 258
 259	/* default SPDIF input sample rate
 260	   to 48000 khz */
 261	ins->spdif_in_sample_rate = 48000;
 262
 263	/* maximize volume */
 264	ins->dac_volume_right = 0x8000;
 265	ins->dac_volume_left = 0x8000;
 266	ins->spdif_input_volume_right = 0x8000;
 267	ins->spdif_input_volume_left = 0x8000;
 268
 269	/* set left and right validity bits and
 270	   default channel status */
 271	ins->spdif_csuv_default =
 272		ins->spdif_csuv_stream =
 273	 /* byte 0 */  ((unsigned int)_wrap_all_bits(  (SNDRV_PCM_DEFAULT_CON_SPDIF        & 0xff)) << 24) |
 274	 /* byte 1 */  ((unsigned int)_wrap_all_bits( ((SNDRV_PCM_DEFAULT_CON_SPDIF >> 8) & 0xff)) << 16) |
 275	 /* byte 3 */   (unsigned int)_wrap_all_bits(  (SNDRV_PCM_DEFAULT_CON_SPDIF >> 24) & 0xff) |
 276	 /* left and right validity bits */ (1 << 13) | (1 << 12);
 277
 278	return ins;
 279
 280error:
 281	kfree(ins->modules);
 282	kfree(ins->code.data);
 283	vfree(ins->symbol_table.symbols);
 284	kfree(ins);
 285	return NULL;
 286}
 287
 288void  cs46xx_dsp_spos_destroy (struct snd_cs46xx * chip)
 289{
 290	int i;
 291	struct dsp_spos_instance * ins = chip->dsp_spos_instance;
 292
 293	if (snd_BUG_ON(!ins))
 294		return;
 295
 296	mutex_lock(&chip->spos_mutex);
 297	for (i = 0; i < ins->nscb; ++i) {
 298		if (ins->scbs[i].deleted) continue;
 299
 300		cs46xx_dsp_proc_free_scb_desc ( (ins->scbs + i) );
 301#ifdef CONFIG_PM_SLEEP
 302		kfree(ins->scbs[i].data);
 303#endif
 304	}
 305
 306	kfree(ins->code.data);
 307	vfree(ins->symbol_table.symbols);
 308	kfree(ins->modules);
 309	kfree(ins);
 310	mutex_unlock(&chip->spos_mutex);
 311}
 312
 313static int dsp_load_parameter(struct snd_cs46xx *chip,
 314			      struct dsp_segment_desc *parameter)
 315{
 316	u32 doffset, dsize;
 317
 318	if (!parameter) {
 319		dev_dbg(chip->card->dev,
 320			"dsp_spos: module got no parameter segment\n");
 321		return 0;
 322	}
 323
 324	doffset = (parameter->offset * 4 + DSP_PARAMETER_BYTE_OFFSET);
 325	dsize   = parameter->size * 4;
 326
 327	dev_dbg(chip->card->dev,
 328		"dsp_spos: downloading parameter data to chip (%08x-%08x)\n",
 329		    doffset,doffset + dsize);
 330	if (snd_cs46xx_download (chip, parameter->data, doffset, dsize)) {
 331		dev_err(chip->card->dev,
 332			"dsp_spos: failed to download parameter data to DSP\n");
 333		return -EINVAL;
 334	}
 335	return 0;
 336}
 337
 338static int dsp_load_sample(struct snd_cs46xx *chip,
 339			   struct dsp_segment_desc *sample)
 340{
 341	u32 doffset, dsize;
 342
 343	if (!sample) {
 344		dev_dbg(chip->card->dev,
 345			"dsp_spos: module got no sample segment\n");
 346		return 0;
 347	}
 348
 349	doffset = (sample->offset * 4  + DSP_SAMPLE_BYTE_OFFSET);
 350	dsize   =  sample->size * 4;
 351
 352	dev_dbg(chip->card->dev,
 353		"dsp_spos: downloading sample data to chip (%08x-%08x)\n",
 354		    doffset,doffset + dsize);
 355
 356	if (snd_cs46xx_download (chip,sample->data,doffset,dsize)) {
 357		dev_err(chip->card->dev,
 358			"dsp_spos: failed to sample data to DSP\n");
 359		return -EINVAL;
 360	}
 361	return 0;
 362}
 363
 364int cs46xx_dsp_load_module (struct snd_cs46xx * chip, struct dsp_module_desc * module)
 365{
 366	struct dsp_spos_instance * ins = chip->dsp_spos_instance;
 367	struct dsp_segment_desc * code = get_segment_desc (module,SEGTYPE_SP_PROGRAM);
 368	u32 doffset, dsize;
 369	int err;
 370
 371	if (ins->nmodules == DSP_MAX_MODULES - 1) {
 372		dev_err(chip->card->dev,
 373			"dsp_spos: to many modules loaded into DSP\n");
 374		return -ENOMEM;
 375	}
 376
 377	dev_dbg(chip->card->dev,
 378		"dsp_spos: loading module %s into DSP\n", module->module_name);
 379  
 380	if (ins->nmodules == 0) {
 381		dev_dbg(chip->card->dev, "dsp_spos: clearing parameter area\n");
 382		snd_cs46xx_clear_BA1(chip, DSP_PARAMETER_BYTE_OFFSET, DSP_PARAMETER_BYTE_SIZE);
 383	}
 384  
 385	err = dsp_load_parameter(chip, get_segment_desc(module,
 386							SEGTYPE_SP_PARAMETER));
 387	if (err < 0)
 388		return err;
 389
 390	if (ins->nmodules == 0) {
 391		dev_dbg(chip->card->dev, "dsp_spos: clearing sample area\n");
 392		snd_cs46xx_clear_BA1(chip, DSP_SAMPLE_BYTE_OFFSET, DSP_SAMPLE_BYTE_SIZE);
 393	}
 394
 395	err = dsp_load_sample(chip, get_segment_desc(module,
 396						     SEGTYPE_SP_SAMPLE));
 397	if (err < 0)
 398		return err;
 399
 400	if (ins->nmodules == 0) {
 401		dev_dbg(chip->card->dev, "dsp_spos: clearing code area\n");
 402		snd_cs46xx_clear_BA1(chip, DSP_CODE_BYTE_OFFSET, DSP_CODE_BYTE_SIZE);
 403	}
 404
 405	if (code == NULL) {
 406		dev_dbg(chip->card->dev,
 407			"dsp_spos: module got no code segment\n");
 408	} else {
 409		if (ins->code.offset + code->size > DSP_CODE_BYTE_SIZE) {
 410			dev_err(chip->card->dev,
 411				"dsp_spos: no space available in DSP\n");
 412			return -ENOMEM;
 413		}
 414
 415		module->load_address = ins->code.offset;
 416		module->overlay_begin_address = 0x000;
 417
 418		/* if module has a code segment it must have
 419		   symbol table */
 420		if (snd_BUG_ON(!module->symbol_table.symbols))
 421			return -ENOMEM;
 422		if (add_symbols(chip,module)) {
 423			dev_err(chip->card->dev,
 424				"dsp_spos: failed to load symbol table\n");
 425			return -ENOMEM;
 426		}
 427    
 428		doffset = (code->offset * 4 + ins->code.offset * 4 + DSP_CODE_BYTE_OFFSET);
 429		dsize   = code->size * 4;
 430		dev_dbg(chip->card->dev,
 431			"dsp_spos: downloading code to chip (%08x-%08x)\n",
 432			    doffset,doffset + dsize);   
 433
 434		module->nfixups = shadow_and_reallocate_code(chip,code->data,code->size,module->overlay_begin_address);
 435
 436		if (snd_cs46xx_download (chip,(ins->code.data + ins->code.offset),doffset,dsize)) {
 437			dev_err(chip->card->dev,
 438				"dsp_spos: failed to download code to DSP\n");
 439			return -EINVAL;
 440		}
 441
 442		ins->code.offset += code->size;
 443	}
 444
 445	/* NOTE: module segments and symbol table must be
 446	   statically allocated. Case that module data is
 447	   not generated by the ospparser */
 448	ins->modules[ins->nmodules] = *module;
 449	ins->nmodules++;
 450
 451	return 0;
 452}
 453
 454struct dsp_symbol_entry *
 455cs46xx_dsp_lookup_symbol (struct snd_cs46xx * chip, char * symbol_name, int symbol_type)
 456{
 457	int i;
 458	struct dsp_spos_instance * ins = chip->dsp_spos_instance;
 459
 460	for ( i = 0; i < ins->symbol_table.nsymbols; ++i ) {
 461
 462		if (ins->symbol_table.symbols[i].deleted)
 463			continue;
 464
 465		if (!strcmp(ins->symbol_table.symbols[i].symbol_name,symbol_name) &&
 466		    ins->symbol_table.symbols[i].symbol_type == symbol_type) {
 467			return (ins->symbol_table.symbols + i);
 468		}
 469	}
 470
 471#if 0
 472	dev_err(chip->card->dev, "dsp_spos: symbol <%s> type %02x not found\n",
 473		symbol_name,symbol_type);
 474#endif
 475
 476	return NULL;
 477}
 478
 479
 480#ifdef CONFIG_SND_PROC_FS
 481static struct dsp_symbol_entry *
 482cs46xx_dsp_lookup_symbol_addr (struct snd_cs46xx * chip, u32 address, int symbol_type)
 483{
 484	int i;
 485	struct dsp_spos_instance * ins = chip->dsp_spos_instance;
 486
 487	for ( i = 0; i < ins->symbol_table.nsymbols; ++i ) {
 488
 489		if (ins->symbol_table.symbols[i].deleted)
 490			continue;
 491
 492		if (ins->symbol_table.symbols[i].address == address &&
 493		    ins->symbol_table.symbols[i].symbol_type == symbol_type) {
 494			return (ins->symbol_table.symbols + i);
 495		}
 496	}
 497
 498
 499	return NULL;
 500}
 501
 502
 503static void cs46xx_dsp_proc_symbol_table_read (struct snd_info_entry *entry,
 504					       struct snd_info_buffer *buffer)
 505{
 506	struct snd_cs46xx *chip = entry->private_data;
 507	struct dsp_spos_instance * ins = chip->dsp_spos_instance;
 508	int i;
 509
 510	snd_iprintf(buffer, "SYMBOLS:\n");
 511	for ( i = 0; i < ins->symbol_table.nsymbols; ++i ) {
 512		char *module_str = "system";
 513
 514		if (ins->symbol_table.symbols[i].deleted)
 515			continue;
 516
 517		if (ins->symbol_table.symbols[i].module != NULL) {
 518			module_str = ins->symbol_table.symbols[i].module->module_name;
 519		}
 520
 521    
 522		snd_iprintf(buffer, "%04X <%02X> %s [%s]\n",
 523			    ins->symbol_table.symbols[i].address,
 524			    ins->symbol_table.symbols[i].symbol_type,
 525			    ins->symbol_table.symbols[i].symbol_name,
 526			    module_str);    
 527	}
 528}
 529
 530
 531static void cs46xx_dsp_proc_modules_read (struct snd_info_entry *entry,
 532					  struct snd_info_buffer *buffer)
 533{
 534	struct snd_cs46xx *chip = entry->private_data;
 535	struct dsp_spos_instance * ins = chip->dsp_spos_instance;
 536	int i,j;
 537
 538	mutex_lock(&chip->spos_mutex);
 539	snd_iprintf(buffer, "MODULES:\n");
 540	for ( i = 0; i < ins->nmodules; ++i ) {
 541		snd_iprintf(buffer, "\n%s:\n", ins->modules[i].module_name);
 542		snd_iprintf(buffer, "   %d symbols\n", ins->modules[i].symbol_table.nsymbols);
 543		snd_iprintf(buffer, "   %d fixups\n", ins->modules[i].nfixups);
 544
 545		for (j = 0; j < ins->modules[i].nsegments; ++ j) {
 546			struct dsp_segment_desc * desc = (ins->modules[i].segments + j);
 547			snd_iprintf(buffer, "   segment %02x offset %08x size %08x\n",
 548				    desc->segment_type,desc->offset, desc->size);
 549		}
 550	}
 551	mutex_unlock(&chip->spos_mutex);
 552}
 553
 554static void cs46xx_dsp_proc_task_tree_read (struct snd_info_entry *entry,
 555					    struct snd_info_buffer *buffer)
 556{
 557	struct snd_cs46xx *chip = entry->private_data;
 558	struct dsp_spos_instance * ins = chip->dsp_spos_instance;
 559	int i, j, col;
 560	void __iomem *dst = chip->region.idx[1].remap_addr + DSP_PARAMETER_BYTE_OFFSET;
 561
 562	mutex_lock(&chip->spos_mutex);
 563	snd_iprintf(buffer, "TASK TREES:\n");
 564	for ( i = 0; i < ins->ntask; ++i) {
 565		snd_iprintf(buffer,"\n%04x %s:\n",ins->tasks[i].address,ins->tasks[i].task_name);
 566
 567		for (col = 0,j = 0;j < ins->tasks[i].size; j++,col++) {
 568			u32 val;
 569			if (col == 4) {
 570				snd_iprintf(buffer,"\n");
 571				col = 0;
 572			}
 573			val = readl(dst + (ins->tasks[i].address + j) * sizeof(u32));
 574			snd_iprintf(buffer,"%08x ",val);
 575		}
 576	}
 577
 578	snd_iprintf(buffer,"\n");  
 579	mutex_unlock(&chip->spos_mutex);
 580}
 581
 582static void cs46xx_dsp_proc_scb_read (struct snd_info_entry *entry,
 583				      struct snd_info_buffer *buffer)
 584{
 585	struct snd_cs46xx *chip = entry->private_data;
 586	struct dsp_spos_instance * ins = chip->dsp_spos_instance;
 587	int i;
 588
 589	mutex_lock(&chip->spos_mutex);
 590	snd_iprintf(buffer, "SCB's:\n");
 591	for ( i = 0; i < ins->nscb; ++i) {
 592		if (ins->scbs[i].deleted)
 593			continue;
 594		snd_iprintf(buffer,"\n%04x %s:\n\n",ins->scbs[i].address,ins->scbs[i].scb_name);
 595
 596		if (ins->scbs[i].parent_scb_ptr != NULL) {
 597			snd_iprintf(buffer,"parent [%s:%04x] ", 
 598				    ins->scbs[i].parent_scb_ptr->scb_name,
 599				    ins->scbs[i].parent_scb_ptr->address);
 600		} else snd_iprintf(buffer,"parent [none] ");
 601
 602		snd_iprintf(buffer,"sub_list_ptr [%s:%04x]\nnext_scb_ptr [%s:%04x]  task_entry [%s:%04x]\n",
 603			    ins->scbs[i].sub_list_ptr->scb_name,
 604			    ins->scbs[i].sub_list_ptr->address,
 605			    ins->scbs[i].next_scb_ptr->scb_name,
 606			    ins->scbs[i].next_scb_ptr->address,
 607			    ins->scbs[i].task_entry->symbol_name,
 608			    ins->scbs[i].task_entry->address);
 609	}
 610
 611	snd_iprintf(buffer,"\n");
 612	mutex_unlock(&chip->spos_mutex);
 613}
 614
 615static void cs46xx_dsp_proc_parameter_dump_read (struct snd_info_entry *entry,
 616						 struct snd_info_buffer *buffer)
 617{
 618	struct snd_cs46xx *chip = entry->private_data;
 619	/*struct dsp_spos_instance * ins = chip->dsp_spos_instance; */
 620	unsigned int i, col = 0;
 621	void __iomem *dst = chip->region.idx[1].remap_addr + DSP_PARAMETER_BYTE_OFFSET;
 622	struct dsp_symbol_entry * symbol; 
 623
 624	for (i = 0;i < DSP_PARAMETER_BYTE_SIZE; i += sizeof(u32),col ++) {
 625		if (col == 4) {
 626			snd_iprintf(buffer,"\n");
 627			col = 0;
 628		}
 629
 630		if ( (symbol = cs46xx_dsp_lookup_symbol_addr (chip,i / sizeof(u32), SYMBOL_PARAMETER)) != NULL) {
 
 631			col = 0;
 632			snd_iprintf (buffer,"\n%s:\n",symbol->symbol_name);
 633		}
 634
 635		if (col == 0) {
 636			snd_iprintf(buffer, "%04X ", i / (unsigned int)sizeof(u32));
 637		}
 638
 639		snd_iprintf(buffer,"%08X ",readl(dst + i));
 640	}
 641}
 642
 643static void cs46xx_dsp_proc_sample_dump_read (struct snd_info_entry *entry,
 644					      struct snd_info_buffer *buffer)
 645{
 646	struct snd_cs46xx *chip = entry->private_data;
 647	int i,col = 0;
 648	void __iomem *dst = chip->region.idx[2].remap_addr;
 649
 650	snd_iprintf(buffer,"PCMREADER:\n");
 651	for (i = PCM_READER_BUF1;i < PCM_READER_BUF1 + 0x30; i += sizeof(u32),col ++) {
 652		if (col == 4) {
 653			snd_iprintf(buffer,"\n");
 654			col = 0;
 655		}
 656
 657		if (col == 0) {
 658			snd_iprintf(buffer, "%04X ",i);
 659		}
 660
 661		snd_iprintf(buffer,"%08X ",readl(dst + i));
 662	}
 663
 664	snd_iprintf(buffer,"\nMIX_SAMPLE_BUF1:\n");
 665
 666	col = 0;
 667	for (i = MIX_SAMPLE_BUF1;i < MIX_SAMPLE_BUF1 + 0x40; i += sizeof(u32),col ++) {
 668		if (col == 4) {
 669			snd_iprintf(buffer,"\n");
 670			col = 0;
 671		}
 672
 673		if (col == 0) {
 674			snd_iprintf(buffer, "%04X ",i);
 675		}
 676
 677		snd_iprintf(buffer,"%08X ",readl(dst + i));
 678	}
 679
 680	snd_iprintf(buffer,"\nSRC_TASK_SCB1:\n");
 681	col = 0;
 682	for (i = 0x2480 ; i < 0x2480 + 0x40 ; i += sizeof(u32),col ++) {
 683		if (col == 4) {
 684			snd_iprintf(buffer,"\n");
 685			col = 0;
 686		}
 687		
 688		if (col == 0) {
 689			snd_iprintf(buffer, "%04X ",i);
 690		}
 691
 692		snd_iprintf(buffer,"%08X ",readl(dst + i));
 693	}
 694
 695
 696	snd_iprintf(buffer,"\nSPDIFO_BUFFER:\n");
 697	col = 0;
 698	for (i = SPDIFO_IP_OUTPUT_BUFFER1;i < SPDIFO_IP_OUTPUT_BUFFER1 + 0x30; i += sizeof(u32),col ++) {
 699		if (col == 4) {
 700			snd_iprintf(buffer,"\n");
 701			col = 0;
 702		}
 703
 704		if (col == 0) {
 705			snd_iprintf(buffer, "%04X ",i);
 706		}
 707
 708		snd_iprintf(buffer,"%08X ",readl(dst + i));
 709	}
 710
 711	snd_iprintf(buffer,"\n...\n");
 712	col = 0;
 713
 714	for (i = SPDIFO_IP_OUTPUT_BUFFER1+0xD0;i < SPDIFO_IP_OUTPUT_BUFFER1 + 0x110; i += sizeof(u32),col ++) {
 715		if (col == 4) {
 716			snd_iprintf(buffer,"\n");
 717			col = 0;
 718		}
 719
 720		if (col == 0) {
 721			snd_iprintf(buffer, "%04X ",i);
 722		}
 723
 724		snd_iprintf(buffer,"%08X ",readl(dst + i));
 725	}
 726
 727
 728	snd_iprintf(buffer,"\nOUTPUT_SNOOP:\n");
 729	col = 0;
 730	for (i = OUTPUT_SNOOP_BUFFER;i < OUTPUT_SNOOP_BUFFER + 0x40; i += sizeof(u32),col ++) {
 731		if (col == 4) {
 732			snd_iprintf(buffer,"\n");
 733			col = 0;
 734		}
 735
 736		if (col == 0) {
 737			snd_iprintf(buffer, "%04X ",i);
 738		}
 739
 740		snd_iprintf(buffer,"%08X ",readl(dst + i));
 741	}
 742
 743	snd_iprintf(buffer,"\nCODEC_INPUT_BUF1: \n");
 744	col = 0;
 745	for (i = CODEC_INPUT_BUF1;i < CODEC_INPUT_BUF1 + 0x40; i += sizeof(u32),col ++) {
 746		if (col == 4) {
 747			snd_iprintf(buffer,"\n");
 748			col = 0;
 749		}
 750
 751		if (col == 0) {
 752			snd_iprintf(buffer, "%04X ",i);
 753		}
 754
 755		snd_iprintf(buffer,"%08X ",readl(dst + i));
 756	}
 757#if 0
 758	snd_iprintf(buffer,"\nWRITE_BACK_BUF1: \n");
 759	col = 0;
 760	for (i = WRITE_BACK_BUF1;i < WRITE_BACK_BUF1 + 0x40; i += sizeof(u32),col ++) {
 761		if (col == 4) {
 762			snd_iprintf(buffer,"\n");
 763			col = 0;
 764		}
 765
 766		if (col == 0) {
 767			snd_iprintf(buffer, "%04X ",i);
 768		}
 769
 770		snd_iprintf(buffer,"%08X ",readl(dst + i));
 771	}
 772#endif
 773
 774	snd_iprintf(buffer,"\nSPDIFI_IP_OUTPUT_BUFFER1: \n");
 775	col = 0;
 776	for (i = SPDIFI_IP_OUTPUT_BUFFER1;i < SPDIFI_IP_OUTPUT_BUFFER1 + 0x80; i += sizeof(u32),col ++) {
 777		if (col == 4) {
 778			snd_iprintf(buffer,"\n");
 779			col = 0;
 780		}
 781
 782		if (col == 0) {
 783			snd_iprintf(buffer, "%04X ",i);
 784		}
 785		
 786		snd_iprintf(buffer,"%08X ",readl(dst + i));
 787	}
 788	snd_iprintf(buffer,"\n");
 789}
 790
 791int cs46xx_dsp_proc_init (struct snd_card *card, struct snd_cs46xx *chip)
 792{
 793	struct snd_info_entry *entry;
 794	struct dsp_spos_instance * ins = chip->dsp_spos_instance;
 795	int i;
 796
 797	ins->snd_card = card;
 798
 799	if ((entry = snd_info_create_card_entry(card, "dsp", card->proc_root)) != NULL) {
 800		entry->content = SNDRV_INFO_CONTENT_TEXT;
 801		entry->mode = S_IFDIR | S_IRUGO | S_IXUGO;
 802      
 803		if (snd_info_register(entry) < 0) {
 804			snd_info_free_entry(entry);
 805			entry = NULL;
 806		}
 807	}
 808
 809	ins->proc_dsp_dir = entry;
 810
 811	if (!ins->proc_dsp_dir)
 812		return -ENOMEM;
 813
 814	if ((entry = snd_info_create_card_entry(card, "spos_symbols", ins->proc_dsp_dir)) != NULL) {
 815		entry->content = SNDRV_INFO_CONTENT_TEXT;
 816		entry->private_data = chip;
 817		entry->mode = S_IFREG | S_IRUGO | S_IWUSR;
 818		entry->c.text.read = cs46xx_dsp_proc_symbol_table_read;
 819		if (snd_info_register(entry) < 0) {
 820			snd_info_free_entry(entry);
 821			entry = NULL;
 822		}
 823	}
 824	ins->proc_sym_info_entry = entry;
 825    
 826	if ((entry = snd_info_create_card_entry(card, "spos_modules", ins->proc_dsp_dir)) != NULL) {
 827		entry->content = SNDRV_INFO_CONTENT_TEXT;
 828		entry->private_data = chip;
 829		entry->mode = S_IFREG | S_IRUGO | S_IWUSR;
 830		entry->c.text.read = cs46xx_dsp_proc_modules_read;
 831		if (snd_info_register(entry) < 0) {
 832			snd_info_free_entry(entry);
 833			entry = NULL;
 834		}
 835	}
 836	ins->proc_modules_info_entry = entry;
 837
 838	if ((entry = snd_info_create_card_entry(card, "parameter", ins->proc_dsp_dir)) != NULL) {
 839		entry->content = SNDRV_INFO_CONTENT_TEXT;
 840		entry->private_data = chip;
 841		entry->mode = S_IFREG | S_IRUGO | S_IWUSR;
 842		entry->c.text.read = cs46xx_dsp_proc_parameter_dump_read;
 843		if (snd_info_register(entry) < 0) {
 844			snd_info_free_entry(entry);
 845			entry = NULL;
 846		}
 847	}
 848	ins->proc_parameter_dump_info_entry = entry;
 849
 850	if ((entry = snd_info_create_card_entry(card, "sample", ins->proc_dsp_dir)) != NULL) {
 851		entry->content = SNDRV_INFO_CONTENT_TEXT;
 852		entry->private_data = chip;
 853		entry->mode = S_IFREG | S_IRUGO | S_IWUSR;
 854		entry->c.text.read = cs46xx_dsp_proc_sample_dump_read;
 855		if (snd_info_register(entry) < 0) {
 856			snd_info_free_entry(entry);
 857			entry = NULL;
 858		}
 859	}
 860	ins->proc_sample_dump_info_entry = entry;
 861
 862	if ((entry = snd_info_create_card_entry(card, "task_tree", ins->proc_dsp_dir)) != NULL) {
 863		entry->content = SNDRV_INFO_CONTENT_TEXT;
 864		entry->private_data = chip;
 865		entry->mode = S_IFREG | S_IRUGO | S_IWUSR;
 866		entry->c.text.read = cs46xx_dsp_proc_task_tree_read;
 867		if (snd_info_register(entry) < 0) {
 868			snd_info_free_entry(entry);
 869			entry = NULL;
 870		}
 871	}
 872	ins->proc_task_info_entry = entry;
 873
 874	if ((entry = snd_info_create_card_entry(card, "scb_info", ins->proc_dsp_dir)) != NULL) {
 875		entry->content = SNDRV_INFO_CONTENT_TEXT;
 876		entry->private_data = chip;
 877		entry->mode = S_IFREG | S_IRUGO | S_IWUSR;
 878		entry->c.text.read = cs46xx_dsp_proc_scb_read;
 879		if (snd_info_register(entry) < 0) {
 880			snd_info_free_entry(entry);
 881			entry = NULL;
 882		}
 883	}
 884	ins->proc_scb_info_entry = entry;
 885
 886	mutex_lock(&chip->spos_mutex);
 887	/* register/update SCB's entries on proc */
 888	for (i = 0; i < ins->nscb; ++i) {
 889		if (ins->scbs[i].deleted) continue;
 890
 891		cs46xx_dsp_proc_register_scb_desc (chip, (ins->scbs + i));
 892	}
 893	mutex_unlock(&chip->spos_mutex);
 894
 895	return 0;
 896}
 897
 898int cs46xx_dsp_proc_done (struct snd_cs46xx *chip)
 899{
 900	struct dsp_spos_instance * ins = chip->dsp_spos_instance;
 901	int i;
 902
 903	snd_info_free_entry(ins->proc_sym_info_entry);
 904	ins->proc_sym_info_entry = NULL;
 905
 906	snd_info_free_entry(ins->proc_modules_info_entry);
 907	ins->proc_modules_info_entry = NULL;
 908
 909	snd_info_free_entry(ins->proc_parameter_dump_info_entry);
 910	ins->proc_parameter_dump_info_entry = NULL;
 911
 912	snd_info_free_entry(ins->proc_sample_dump_info_entry);
 913	ins->proc_sample_dump_info_entry = NULL;
 914
 915	snd_info_free_entry(ins->proc_scb_info_entry);
 916	ins->proc_scb_info_entry = NULL;
 917
 918	snd_info_free_entry(ins->proc_task_info_entry);
 919	ins->proc_task_info_entry = NULL;
 920
 921	mutex_lock(&chip->spos_mutex);
 922	for (i = 0; i < ins->nscb; ++i) {
 923		if (ins->scbs[i].deleted) continue;
 924		cs46xx_dsp_proc_free_scb_desc ( (ins->scbs + i) );
 925	}
 926	mutex_unlock(&chip->spos_mutex);
 927
 928	snd_info_free_entry(ins->proc_dsp_dir);
 929	ins->proc_dsp_dir = NULL;
 930
 931	return 0;
 932}
 933#endif /* CONFIG_SND_PROC_FS */
 934
 935static void _dsp_create_task_tree (struct snd_cs46xx *chip, u32 * task_data,
 936				   u32  dest, int size)
 937{
 938	void __iomem *spdst = chip->region.idx[1].remap_addr + 
 939		DSP_PARAMETER_BYTE_OFFSET + dest * sizeof(u32);
 940	int i;
 941
 942	for (i = 0; i < size; ++i) {
 943		dev_dbg(chip->card->dev, "addr %p, val %08x\n",
 944			spdst, task_data[i]);
 945		writel(task_data[i],spdst);
 946		spdst += sizeof(u32);
 947	}
 948}
 949
 950static void _dsp_create_scb (struct snd_cs46xx *chip, u32 * scb_data, u32 dest)
 951{
 952	void __iomem *spdst = chip->region.idx[1].remap_addr + 
 953		DSP_PARAMETER_BYTE_OFFSET + dest * sizeof(u32);
 954	int i;
 955
 956	for (i = 0; i < 0x10; ++i) {
 957		dev_dbg(chip->card->dev, "addr %p, val %08x\n",
 958			spdst, scb_data[i]);
 959		writel(scb_data[i],spdst);
 960		spdst += sizeof(u32);
 961	}
 962}
 963
 964static int find_free_scb_index (struct dsp_spos_instance * ins)
 965{
 966	int index = ins->nscb, i;
 967
 968	for (i = ins->scb_highest_frag_index; i < ins->nscb; ++i) {
 969		if (ins->scbs[i].deleted) {
 970			index = i;
 971			break;
 972		}
 973	}
 974
 975	return index;
 976}
 977
 978static struct dsp_scb_descriptor * _map_scb (struct snd_cs46xx *chip, char * name, u32 dest)
 979{
 980	struct dsp_spos_instance * ins = chip->dsp_spos_instance;
 981	struct dsp_scb_descriptor * desc = NULL;
 982	int index;
 983
 984	if (ins->nscb == DSP_MAX_SCB_DESC - 1) {
 985		dev_err(chip->card->dev,
 986			"dsp_spos: got no place for other SCB\n");
 987		return NULL;
 988	}
 989
 990	index = find_free_scb_index (ins);
 991
 992	memset(&ins->scbs[index], 0, sizeof(ins->scbs[index]));
 993	strcpy(ins->scbs[index].scb_name, name);
 994	ins->scbs[index].address = dest;
 995	ins->scbs[index].index = index;
 996	ins->scbs[index].ref_count = 1;
 997
 998	desc = (ins->scbs + index);
 999	ins->scbs[index].scb_symbol = add_symbol (chip, name, dest, SYMBOL_PARAMETER);
1000
1001	if (index > ins->scb_highest_frag_index)
1002		ins->scb_highest_frag_index = index;
1003
1004	if (index == ins->nscb)
1005		ins->nscb++;
1006
1007	return desc;
1008}
1009
1010static struct dsp_task_descriptor *
1011_map_task_tree (struct snd_cs46xx *chip, char * name, u32 dest, u32 size)
1012{
1013	struct dsp_spos_instance * ins = chip->dsp_spos_instance;
1014	struct dsp_task_descriptor * desc = NULL;
1015
1016	if (ins->ntask == DSP_MAX_TASK_DESC - 1) {
1017		dev_err(chip->card->dev,
1018			"dsp_spos: got no place for other TASK\n");
1019		return NULL;
1020	}
1021
1022	if (name)
1023		strcpy(ins->tasks[ins->ntask].task_name, name);
1024	else
1025		strcpy(ins->tasks[ins->ntask].task_name, "(NULL)");
1026	ins->tasks[ins->ntask].address = dest;
1027	ins->tasks[ins->ntask].size = size;
1028
1029	/* quick find in list */
1030	ins->tasks[ins->ntask].index = ins->ntask;
1031	desc = (ins->tasks + ins->ntask);
1032	ins->ntask++;
1033
1034	if (name)
1035		add_symbol (chip,name,dest,SYMBOL_PARAMETER);
1036	return desc;
1037}
1038
1039#define SCB_BYTES	(0x10 * 4)
1040
1041struct dsp_scb_descriptor *
1042cs46xx_dsp_create_scb (struct snd_cs46xx *chip, char * name, u32 * scb_data, u32 dest)
1043{
1044	struct dsp_scb_descriptor * desc;
1045
1046#ifdef CONFIG_PM_SLEEP
1047	/* copy the data for resume */
1048	scb_data = kmemdup(scb_data, SCB_BYTES, GFP_KERNEL);
1049	if (!scb_data)
1050		return NULL;
1051#endif
1052
1053	desc = _map_scb (chip,name,dest);
1054	if (desc) {
1055		desc->data = scb_data;
1056		_dsp_create_scb(chip,scb_data,dest);
1057	} else {
1058		dev_err(chip->card->dev, "dsp_spos: failed to map SCB\n");
1059#ifdef CONFIG_PM_SLEEP
1060		kfree(scb_data);
1061#endif
1062	}
1063
1064	return desc;
1065}
1066
1067
1068static struct dsp_task_descriptor *
1069cs46xx_dsp_create_task_tree (struct snd_cs46xx *chip, char * name, u32 * task_data,
1070			     u32 dest, int size)
1071{
1072	struct dsp_task_descriptor * desc;
1073
1074	desc = _map_task_tree (chip,name,dest,size);
1075	if (desc) {
1076		desc->data = task_data;
1077		_dsp_create_task_tree(chip,task_data,dest,size);
1078	} else {
1079		dev_err(chip->card->dev, "dsp_spos: failed to map TASK\n");
1080	}
1081
1082	return desc;
1083}
1084
1085int cs46xx_dsp_scb_and_task_init (struct snd_cs46xx *chip)
1086{
1087	struct dsp_spos_instance * ins = chip->dsp_spos_instance;
1088	struct dsp_symbol_entry * fg_task_tree_header_code;
1089	struct dsp_symbol_entry * task_tree_header_code;
1090	struct dsp_symbol_entry * task_tree_thread;
1091	struct dsp_symbol_entry * null_algorithm;
1092	struct dsp_symbol_entry * magic_snoop_task;
1093
1094	struct dsp_scb_descriptor * timing_master_scb;
1095	struct dsp_scb_descriptor * codec_out_scb;
1096	struct dsp_scb_descriptor * codec_in_scb;
1097	struct dsp_scb_descriptor * src_task_scb;
1098	struct dsp_scb_descriptor * master_mix_scb;
1099	struct dsp_scb_descriptor * rear_mix_scb;
1100	struct dsp_scb_descriptor * record_mix_scb;
1101	struct dsp_scb_descriptor * write_back_scb;
1102	struct dsp_scb_descriptor * vari_decimate_scb;
1103	struct dsp_scb_descriptor * rear_codec_out_scb;
1104	struct dsp_scb_descriptor * clfe_codec_out_scb;
1105	struct dsp_scb_descriptor * magic_snoop_scb;
1106	
1107	int fifo_addr, fifo_span, valid_slots;
1108
1109	static struct dsp_spos_control_block sposcb = {
1110		/* 0 */ HFG_TREE_SCB,HFG_STACK,
1111		/* 1 */ SPOSCB_ADDR,BG_TREE_SCB_ADDR,
1112		/* 2 */ DSP_SPOS_DC,0,
1113		/* 3 */ DSP_SPOS_DC,DSP_SPOS_DC,
1114		/* 4 */ 0,0,
1115		/* 5 */ DSP_SPOS_UU,0,
1116		/* 6 */ FG_TASK_HEADER_ADDR,0,
1117		/* 7 */ 0,0,
1118		/* 8 */ DSP_SPOS_UU,DSP_SPOS_DC,
1119		/* 9 */ 0,
1120		/* A */ 0,HFG_FIRST_EXECUTE_MODE,
1121		/* B */ DSP_SPOS_UU,DSP_SPOS_UU,
1122		/* C */ DSP_SPOS_DC_DC,
1123		/* D */ DSP_SPOS_DC_DC,
1124		/* E */ DSP_SPOS_DC_DC,
1125		/* F */ DSP_SPOS_DC_DC
1126	};
1127
1128	cs46xx_dsp_create_task_tree(chip, "sposCB", (u32 *)&sposcb, SPOSCB_ADDR, 0x10);
1129
1130	null_algorithm  = cs46xx_dsp_lookup_symbol(chip, "NULLALGORITHM", SYMBOL_CODE);
1131	if (null_algorithm == NULL) {
1132		dev_err(chip->card->dev,
1133			"dsp_spos: symbol NULLALGORITHM not found\n");
1134		return -EIO;
1135	}
1136
1137	fg_task_tree_header_code = cs46xx_dsp_lookup_symbol(chip, "FGTASKTREEHEADERCODE", SYMBOL_CODE);  
1138	if (fg_task_tree_header_code == NULL) {
1139		dev_err(chip->card->dev,
1140			"dsp_spos: symbol FGTASKTREEHEADERCODE not found\n");
1141		return -EIO;
1142	}
1143
1144	task_tree_header_code = cs46xx_dsp_lookup_symbol(chip, "TASKTREEHEADERCODE", SYMBOL_CODE);  
1145	if (task_tree_header_code == NULL) {
1146		dev_err(chip->card->dev,
1147			"dsp_spos: symbol TASKTREEHEADERCODE not found\n");
1148		return -EIO;
1149	}
1150  
1151	task_tree_thread = cs46xx_dsp_lookup_symbol(chip, "TASKTREETHREAD", SYMBOL_CODE);
1152	if (task_tree_thread == NULL) {
1153		dev_err(chip->card->dev,
1154			"dsp_spos: symbol TASKTREETHREAD not found\n");
1155		return -EIO;
1156	}
1157
1158	magic_snoop_task = cs46xx_dsp_lookup_symbol(chip, "MAGICSNOOPTASK", SYMBOL_CODE);
1159	if (magic_snoop_task == NULL) {
1160		dev_err(chip->card->dev,
1161			"dsp_spos: symbol MAGICSNOOPTASK not found\n");
1162		return -EIO;
1163	}
1164  
1165	{
1166		/* create the null SCB */
1167		static struct dsp_generic_scb null_scb = {
1168			{ 0, 0, 0, 0 },
1169			{ 0, 0, 0, 0, 0 },
1170			NULL_SCB_ADDR, NULL_SCB_ADDR,
1171			0, 0, 0, 0, 0,
1172			{
1173				0,0,
1174				0,0,
1175			}
1176		};
1177
1178		null_scb.entry_point = null_algorithm->address;
1179		ins->the_null_scb = cs46xx_dsp_create_scb(chip, "nullSCB", (u32 *)&null_scb, NULL_SCB_ADDR);
1180		ins->the_null_scb->task_entry = null_algorithm;
1181		ins->the_null_scb->sub_list_ptr = ins->the_null_scb;
1182		ins->the_null_scb->next_scb_ptr = ins->the_null_scb;
1183		ins->the_null_scb->parent_scb_ptr = NULL;
1184		cs46xx_dsp_proc_register_scb_desc (chip,ins->the_null_scb);
1185	}
1186
1187	{
1188		/* setup foreground task tree */
1189		static struct dsp_task_tree_control_block fg_task_tree_hdr =  {
1190			{ FG_TASK_HEADER_ADDR | (DSP_SPOS_DC << 0x10),
1191			  DSP_SPOS_DC_DC,
1192			  DSP_SPOS_DC_DC,
1193			  0x0000,DSP_SPOS_DC,
1194			  DSP_SPOS_DC, DSP_SPOS_DC,
1195			  DSP_SPOS_DC_DC,
1196			  DSP_SPOS_DC_DC,
1197			  DSP_SPOS_DC_DC,
1198			  DSP_SPOS_DC,DSP_SPOS_DC },
1199    
1200			{
1201				BG_TREE_SCB_ADDR,TIMINGMASTER_SCB_ADDR, 
1202				0,
1203				FG_TASK_HEADER_ADDR + TCBData,                  
1204			},
1205
1206			{    
1207				4,0,
1208				1,0,
1209				2,SPOSCB_ADDR + HFGFlags,
1210				0,0,
1211				FG_TASK_HEADER_ADDR + TCBContextBlk,FG_STACK
1212			},
1213
1214			{
1215				DSP_SPOS_DC,0,
1216				DSP_SPOS_DC,DSP_SPOS_DC,
1217				DSP_SPOS_DC,DSP_SPOS_DC,
1218				DSP_SPOS_DC,DSP_SPOS_DC,
1219				DSP_SPOS_DC,DSP_SPOS_DC,
1220				DSP_SPOS_DCDC,
1221				DSP_SPOS_UU,1,
1222				DSP_SPOS_DCDC,
1223				DSP_SPOS_DCDC,
1224				DSP_SPOS_DCDC,
1225				DSP_SPOS_DCDC,
1226				DSP_SPOS_DCDC,
1227				DSP_SPOS_DCDC,
1228				DSP_SPOS_DCDC,
1229				DSP_SPOS_DCDC,
1230				DSP_SPOS_DCDC,
1231				DSP_SPOS_DCDC,
1232				DSP_SPOS_DCDC,
1233				DSP_SPOS_DCDC,
1234				DSP_SPOS_DCDC,
1235				DSP_SPOS_DCDC,
1236				DSP_SPOS_DCDC,
1237				DSP_SPOS_DCDC,
1238				DSP_SPOS_DCDC,
1239				DSP_SPOS_DCDC,
1240				DSP_SPOS_DCDC,
1241				DSP_SPOS_DCDC,
1242				DSP_SPOS_DCDC,
1243				DSP_SPOS_DCDC,
1244				DSP_SPOS_DCDC,
1245				DSP_SPOS_DCDC,
1246				DSP_SPOS_DCDC,
1247				DSP_SPOS_DCDC,
1248				DSP_SPOS_DCDC,
1249				DSP_SPOS_DCDC 
1250			},                                               
1251			{ 
1252				FG_INTERVAL_TIMER_PERIOD,DSP_SPOS_UU,
1253				0,0
1254			}
1255		};
1256
1257		fg_task_tree_hdr.links.entry_point = fg_task_tree_header_code->address;
1258		fg_task_tree_hdr.context_blk.stack0 = task_tree_thread->address;
1259		cs46xx_dsp_create_task_tree(chip,"FGtaskTreeHdr",(u32 *)&fg_task_tree_hdr,FG_TASK_HEADER_ADDR,0x35);
1260	}
1261
1262
1263	{
1264		/* setup foreground task tree */
1265		static struct dsp_task_tree_control_block bg_task_tree_hdr =  {
1266			{ DSP_SPOS_DC_DC,
1267			  DSP_SPOS_DC_DC,
1268			  DSP_SPOS_DC_DC,
1269			  DSP_SPOS_DC, DSP_SPOS_DC,
1270			  DSP_SPOS_DC, DSP_SPOS_DC,
1271			  DSP_SPOS_DC_DC,
1272			  DSP_SPOS_DC_DC,
1273			  DSP_SPOS_DC_DC,
1274			  DSP_SPOS_DC,DSP_SPOS_DC },
1275    
1276			{
1277				NULL_SCB_ADDR,NULL_SCB_ADDR,  /* Set up the background to do nothing */
1278				0,
1279				BG_TREE_SCB_ADDR + TCBData,
1280			},
1281
1282			{    
1283				9999,0,
1284				0,1,
1285				0,SPOSCB_ADDR + HFGFlags,
1286				0,0,
1287				BG_TREE_SCB_ADDR + TCBContextBlk,BG_STACK
1288			},
1289
1290			{
1291				DSP_SPOS_DC,0,
1292				DSP_SPOS_DC,DSP_SPOS_DC,
1293				DSP_SPOS_DC,DSP_SPOS_DC,
1294				DSP_SPOS_DC,DSP_SPOS_DC,
1295				DSP_SPOS_DC,DSP_SPOS_DC,
1296				DSP_SPOS_DCDC,
1297				DSP_SPOS_UU,1,
1298				DSP_SPOS_DCDC,
1299				DSP_SPOS_DCDC,
1300				DSP_SPOS_DCDC,
1301				DSP_SPOS_DCDC,
1302				DSP_SPOS_DCDC,
1303				DSP_SPOS_DCDC,
1304				DSP_SPOS_DCDC,
1305				DSP_SPOS_DCDC,
1306				DSP_SPOS_DCDC,
1307				DSP_SPOS_DCDC,
1308				DSP_SPOS_DCDC,
1309				DSP_SPOS_DCDC,
1310				DSP_SPOS_DCDC,
1311				DSP_SPOS_DCDC,
1312				DSP_SPOS_DCDC,
1313				DSP_SPOS_DCDC,
1314				DSP_SPOS_DCDC,
1315				DSP_SPOS_DCDC,
1316				DSP_SPOS_DCDC,
1317				DSP_SPOS_DCDC,
1318				DSP_SPOS_DCDC,
1319				DSP_SPOS_DCDC,
1320				DSP_SPOS_DCDC,
1321				DSP_SPOS_DCDC,
1322				DSP_SPOS_DCDC,
1323				DSP_SPOS_DCDC,
1324				DSP_SPOS_DCDC,
1325				DSP_SPOS_DCDC 
1326			},                                               
1327			{ 
1328				BG_INTERVAL_TIMER_PERIOD,DSP_SPOS_UU,
1329				0,0
1330			}
1331		};
1332
1333		bg_task_tree_hdr.links.entry_point = task_tree_header_code->address;
1334		bg_task_tree_hdr.context_blk.stack0 = task_tree_thread->address;
1335		cs46xx_dsp_create_task_tree(chip,"BGtaskTreeHdr",(u32 *)&bg_task_tree_hdr,BG_TREE_SCB_ADDR,0x35);
1336	}
1337
1338	/* create timing master SCB */
1339	timing_master_scb = cs46xx_dsp_create_timing_master_scb(chip);
1340
1341	/* create the CODEC output task */
1342	codec_out_scb = cs46xx_dsp_create_codec_out_scb(chip,"CodecOutSCB_I",0x0010,0x0000,
1343							MASTERMIX_SCB_ADDR,
1344							CODECOUT_SCB_ADDR,timing_master_scb,
1345							SCB_ON_PARENT_SUBLIST_SCB);
1346
1347	if (!codec_out_scb) goto _fail_end;
1348	/* create the master mix SCB */
1349	master_mix_scb = cs46xx_dsp_create_mix_only_scb(chip,"MasterMixSCB",
1350							MIX_SAMPLE_BUF1,MASTERMIX_SCB_ADDR,
1351							codec_out_scb,
1352							SCB_ON_PARENT_SUBLIST_SCB);
1353	ins->master_mix_scb = master_mix_scb;
1354
1355	if (!master_mix_scb) goto _fail_end;
1356
1357	/* create codec in */
1358	codec_in_scb = cs46xx_dsp_create_codec_in_scb(chip,"CodecInSCB",0x0010,0x00A0,
1359						      CODEC_INPUT_BUF1,
1360						      CODECIN_SCB_ADDR,codec_out_scb,
1361						      SCB_ON_PARENT_NEXT_SCB);
1362	if (!codec_in_scb) goto _fail_end;
1363	ins->codec_in_scb = codec_in_scb;
1364
1365	/* create write back scb */
1366	write_back_scb = cs46xx_dsp_create_mix_to_ostream_scb(chip,"WriteBackSCB",
1367							      WRITE_BACK_BUF1,WRITE_BACK_SPB,
1368							      WRITEBACK_SCB_ADDR,
1369							      timing_master_scb,
1370							      SCB_ON_PARENT_NEXT_SCB);
1371	if (!write_back_scb) goto _fail_end;
1372
1373	{
1374		static struct dsp_mix2_ostream_spb mix2_ostream_spb = {
1375			0x00020000,
1376			0x0000ffff
1377		};
1378    
1379		if (!cs46xx_dsp_create_task_tree(chip, NULL,
1380						 (u32 *)&mix2_ostream_spb,
1381						 WRITE_BACK_SPB, 2))
1382			goto _fail_end;
1383	}
1384
1385	/* input sample converter */
1386	vari_decimate_scb = cs46xx_dsp_create_vari_decimate_scb(chip,"VariDecimateSCB",
1387								VARI_DECIMATE_BUF0,
1388								VARI_DECIMATE_BUF1,
1389								VARIDECIMATE_SCB_ADDR,
1390								write_back_scb,
1391								SCB_ON_PARENT_SUBLIST_SCB);
1392	if (!vari_decimate_scb) goto _fail_end;
1393
1394	/* create the record mixer SCB */
1395	record_mix_scb = cs46xx_dsp_create_mix_only_scb(chip,"RecordMixerSCB",
1396							MIX_SAMPLE_BUF2,
1397							RECORD_MIXER_SCB_ADDR,
1398							vari_decimate_scb,
1399							SCB_ON_PARENT_SUBLIST_SCB);
1400	ins->record_mixer_scb = record_mix_scb;
1401
1402	if (!record_mix_scb) goto _fail_end;
1403
1404	valid_slots = snd_cs46xx_peekBA0(chip, BA0_ACOSV);
1405
1406	if (snd_BUG_ON(chip->nr_ac97_codecs != 1 && chip->nr_ac97_codecs != 2))
1407		goto _fail_end;
1408
1409	if (chip->nr_ac97_codecs == 1) {
1410		/* output on slot 5 and 11 
1411		   on primary CODEC */
1412		fifo_addr = 0x20;
1413		fifo_span = 0x60;
1414
1415		/* enable slot 5 and 11 */
1416		valid_slots |= ACOSV_SLV5 | ACOSV_SLV11;
1417	} else {
1418		/* output on slot 7 and 8 
1419		   on secondary CODEC */
1420		fifo_addr = 0x40;
1421		fifo_span = 0x10;
1422
1423		/* enable slot 7 and 8 */
1424		valid_slots |= ACOSV_SLV7 | ACOSV_SLV8;
1425	}
1426	/* create CODEC tasklet for rear speakers output*/
1427	rear_codec_out_scb = cs46xx_dsp_create_codec_out_scb(chip,"CodecOutSCB_Rear",fifo_span,fifo_addr,
1428							     REAR_MIXER_SCB_ADDR,
1429							     REAR_CODECOUT_SCB_ADDR,codec_in_scb,
1430							     SCB_ON_PARENT_NEXT_SCB);
1431	if (!rear_codec_out_scb) goto _fail_end;
1432	
1433	
1434	/* create the rear PCM channel  mixer SCB */
1435	rear_mix_scb = cs46xx_dsp_create_mix_only_scb(chip,"RearMixerSCB",
1436						      MIX_SAMPLE_BUF3,
1437						      REAR_MIXER_SCB_ADDR,
1438						      rear_codec_out_scb,
1439						      SCB_ON_PARENT_SUBLIST_SCB);
1440	ins->rear_mix_scb = rear_mix_scb;
1441	if (!rear_mix_scb) goto _fail_end;
1442	
1443	if (chip->nr_ac97_codecs == 2) {
1444		/* create CODEC tasklet for rear Center/LFE output 
1445		   slot 6 and 9 on secondary CODEC */
1446		clfe_codec_out_scb = cs46xx_dsp_create_codec_out_scb(chip,"CodecOutSCB_CLFE",0x0030,0x0030,
1447								     CLFE_MIXER_SCB_ADDR,
1448								     CLFE_CODEC_SCB_ADDR,
1449								     rear_codec_out_scb,
1450								     SCB_ON_PARENT_NEXT_SCB);
1451		if (!clfe_codec_out_scb) goto _fail_end;
1452		
1453		
1454		/* create the rear PCM channel  mixer SCB */
1455		ins->center_lfe_mix_scb = cs46xx_dsp_create_mix_only_scb(chip,"CLFEMixerSCB",
1456									 MIX_SAMPLE_BUF4,
1457									 CLFE_MIXER_SCB_ADDR,
1458									 clfe_codec_out_scb,
1459									 SCB_ON_PARENT_SUBLIST_SCB);
1460		if (!ins->center_lfe_mix_scb) goto _fail_end;
1461
1462		/* enable slot 6 and 9 */
1463		valid_slots |= ACOSV_SLV6 | ACOSV_SLV9;
1464	} else {
1465		clfe_codec_out_scb = rear_codec_out_scb;
1466		ins->center_lfe_mix_scb = rear_mix_scb;
1467	}
1468
1469	/* enable slots depending on CODEC configuration */
1470	snd_cs46xx_pokeBA0(chip, BA0_ACOSV, valid_slots);
1471
1472	/* the magic snooper */
1473	magic_snoop_scb = cs46xx_dsp_create_magic_snoop_scb (chip,"MagicSnoopSCB_I",OUTPUTSNOOP_SCB_ADDR,
1474							     OUTPUT_SNOOP_BUFFER,
1475							     codec_out_scb,
1476							     clfe_codec_out_scb,
1477							     SCB_ON_PARENT_NEXT_SCB);
1478
1479    
1480	if (!magic_snoop_scb) goto _fail_end;
1481	ins->ref_snoop_scb = magic_snoop_scb;
1482
1483	/* SP IO access */
1484	if (!cs46xx_dsp_create_spio_write_scb(chip,"SPIOWriteSCB",SPIOWRITE_SCB_ADDR,
1485					      magic_snoop_scb,
1486					      SCB_ON_PARENT_NEXT_SCB))
1487		goto _fail_end;
1488
1489	/* SPDIF input sampel rate converter */
1490	src_task_scb = cs46xx_dsp_create_src_task_scb(chip,"SrcTaskSCB_SPDIFI",
1491						      ins->spdif_in_sample_rate,
1492						      SRC_OUTPUT_BUF1,
1493						      SRC_DELAY_BUF1,SRCTASK_SCB_ADDR,
1494						      master_mix_scb,
1495						      SCB_ON_PARENT_SUBLIST_SCB,1);
1496
1497	if (!src_task_scb) goto _fail_end;
1498	cs46xx_src_unlink(chip,src_task_scb);
1499
1500	/* NOTE: when we now how to detect the SPDIF input
1501	   sample rate we will use this SRC to adjust it */
1502	ins->spdif_in_src = src_task_scb;
1503
1504	cs46xx_dsp_async_init(chip,timing_master_scb);
1505	return 0;
1506
1507 _fail_end:
1508	dev_err(chip->card->dev, "dsp_spos: failed to setup SCB's in DSP\n");
1509	return -EINVAL;
1510}
1511
1512static int cs46xx_dsp_async_init (struct snd_cs46xx *chip,
1513				  struct dsp_scb_descriptor * fg_entry)
1514{
1515	struct dsp_spos_instance * ins = chip->dsp_spos_instance;
1516	struct dsp_symbol_entry * s16_async_codec_input_task;
1517	struct dsp_symbol_entry * spdifo_task;
1518	struct dsp_symbol_entry * spdifi_task;
1519	struct dsp_scb_descriptor * spdifi_scb_desc, * spdifo_scb_desc, * async_codec_scb_desc;
1520
1521	s16_async_codec_input_task = cs46xx_dsp_lookup_symbol(chip, "S16_ASYNCCODECINPUTTASK", SYMBOL_CODE);
1522	if (s16_async_codec_input_task == NULL) {
1523		dev_err(chip->card->dev,
1524			"dsp_spos: symbol S16_ASYNCCODECINPUTTASK not found\n");
1525		return -EIO;
1526	}
1527	spdifo_task = cs46xx_dsp_lookup_symbol(chip, "SPDIFOTASK", SYMBOL_CODE);
1528	if (spdifo_task == NULL) {
1529		dev_err(chip->card->dev,
1530			"dsp_spos: symbol SPDIFOTASK not found\n");
1531		return -EIO;
1532	}
1533
1534	spdifi_task = cs46xx_dsp_lookup_symbol(chip, "SPDIFITASK", SYMBOL_CODE);
1535	if (spdifi_task == NULL) {
1536		dev_err(chip->card->dev,
1537			"dsp_spos: symbol SPDIFITASK not found\n");
1538		return -EIO;
1539	}
1540
1541	{
1542		/* 0xBC0 */
1543		struct dsp_spdifoscb spdifo_scb = {
1544			/* 0 */ DSP_SPOS_UUUU,
1545			{
1546				/* 1 */ 0xb0, 
1547				/* 2 */ 0, 
1548				/* 3 */ 0, 
1549				/* 4 */ 0, 
1550			},
1551			/* NOTE: the SPDIF output task read samples in mono
1552			   format, the AsynchFGTxSCB task writes to buffer
1553			   in stereo format
1554			*/
1555			/* 5 */ RSCONFIG_SAMPLE_16MONO + RSCONFIG_MODULO_256,
1556			/* 6 */ ( SPDIFO_IP_OUTPUT_BUFFER1 << 0x10 )  |  0xFFFC,
1557			/* 7 */ 0,0, 
1558			/* 8 */ 0, 
1559			/* 9 */ FG_TASK_HEADER_ADDR, NULL_SCB_ADDR, 
1560			/* A */ spdifo_task->address,
1561			SPDIFO_SCB_INST + SPDIFOFIFOPointer,
1562			{
1563				/* B */ 0x0040, /*DSP_SPOS_UUUU,*/
1564				/* C */ 0x20ff, /*DSP_SPOS_UUUU,*/
1565			},
1566			/* D */ 0x804c,0,							  /* SPDIFOFIFOPointer:SPDIFOStatRegAddr; */
1567			/* E */ 0x0108,0x0001,					  /* SPDIFOStMoFormat:SPDIFOFIFOBaseAddr; */
1568			/* F */ DSP_SPOS_UUUU	  			          /* SPDIFOFree; */
1569		};
1570
1571		/* 0xBB0 */
1572		struct dsp_spdifiscb spdifi_scb = {
1573			/* 0 */ DSP_SPOS_UULO,DSP_SPOS_UUHI,
1574			/* 1 */ 0,
1575			/* 2 */ 0,
1576			/* 3 */ 1,4000,        /* SPDIFICountLimit SPDIFICount */ 
1577			/* 4 */ DSP_SPOS_UUUU, /* SPDIFIStatusData */
1578			/* 5 */ 0,DSP_SPOS_UUHI, /* StatusData, Free4 */
1579			/* 6 */ DSP_SPOS_UUUU,  /* Free3 */
1580			/* 7 */ DSP_SPOS_UU,DSP_SPOS_DC,  /* Free2 BitCount*/
1581			/* 8 */ DSP_SPOS_UUUU,	/* TempStatus */
1582			/* 9 */ SPDIFO_SCB_INST, NULL_SCB_ADDR,
1583			/* A */ spdifi_task->address,
1584			SPDIFI_SCB_INST + SPDIFIFIFOPointer,
1585			/* NOTE: The SPDIF input task write the sample in mono
1586			   format from the HW FIFO, the AsynchFGRxSCB task  reads 
1587			   them in stereo 
1588			*/
1589			/* B */ RSCONFIG_SAMPLE_16MONO + RSCONFIG_MODULO_128,
1590			/* C */ (SPDIFI_IP_OUTPUT_BUFFER1 << 0x10) | 0xFFFC,
1591			/* D */ 0x8048,0,
1592			/* E */ 0x01f0,0x0001,
1593			/* F */ DSP_SPOS_UUUU /* SPDIN_STATUS monitor */
1594		};
1595
1596		/* 0xBA0 */
1597		struct dsp_async_codec_input_scb async_codec_input_scb = {
1598			/* 0 */ DSP_SPOS_UUUU,
1599			/* 1 */ 0,
1600			/* 2 */ 0,
1601			/* 3 */ 1,4000,
1602			/* 4 */ 0x0118,0x0001,
1603			/* 5 */ RSCONFIG_SAMPLE_16MONO + RSCONFIG_MODULO_64,
1604			/* 6 */ (ASYNC_IP_OUTPUT_BUFFER1 << 0x10) | 0xFFFC,
1605			/* 7 */ DSP_SPOS_UU,0x3,
1606			/* 8 */ DSP_SPOS_UUUU,
1607			/* 9 */ SPDIFI_SCB_INST,NULL_SCB_ADDR,
1608			/* A */ s16_async_codec_input_task->address,
1609			HFG_TREE_SCB + AsyncCIOFIFOPointer,
1610              
1611			/* B */ RSCONFIG_SAMPLE_16STEREO + RSCONFIG_MODULO_64,
1612			/* C */ (ASYNC_IP_OUTPUT_BUFFER1 << 0x10),  /*(ASYNC_IP_OUTPUT_BUFFER1 << 0x10) | 0xFFFC,*/
1613      
1614#ifdef UseASER1Input
1615			/* short AsyncCIFIFOPointer:AsyncCIStatRegAddr;	       
1616			   Init. 0000:8042: for ASER1
1617			   0000:8044: for ASER2 */
1618			/* D */ 0x8042,0,
1619      
1620			/* short AsyncCIStMoFormat:AsyncCIFIFOBaseAddr;
1621			   Init 1 stero:8050 ASER1
1622			   Init 0  mono:8070 ASER2
1623			   Init 1 Stereo : 0100 ASER1 (Set by script) */
1624			/* E */ 0x0100,0x0001,
1625      
1626#endif
1627      
1628#ifdef UseASER2Input
1629			/* short AsyncCIFIFOPointer:AsyncCIStatRegAddr;
1630			   Init. 0000:8042: for ASER1
1631			   0000:8044: for ASER2 */
1632			/* D */ 0x8044,0,
1633      
1634			/* short AsyncCIStMoFormat:AsyncCIFIFOBaseAddr;
1635			   Init 1 stero:8050 ASER1
1636			   Init 0  mono:8070 ASER2
1637			   Init 1 Stereo : 0100 ASER1 (Set by script) */
1638			/* E */ 0x0110,0x0001,
1639      
1640#endif
1641      
1642			/* short AsyncCIOutputBufModulo:AsyncCIFree;
1643			   AsyncCIOutputBufModulo: The modulo size for   
1644			   the output buffer of this task */
1645			/* F */ 0, /* DSP_SPOS_UUUU */
1646		};
1647
1648		spdifo_scb_desc = cs46xx_dsp_create_scb(chip,"SPDIFOSCB",(u32 *)&spdifo_scb,SPDIFO_SCB_INST);
1649
1650		if (snd_BUG_ON(!spdifo_scb_desc))
1651			return -EIO;
1652		spdifi_scb_desc = cs46xx_dsp_create_scb(chip,"SPDIFISCB",(u32 *)&spdifi_scb,SPDIFI_SCB_INST);
1653		if (snd_BUG_ON(!spdifi_scb_desc))
1654			return -EIO;
1655		async_codec_scb_desc = cs46xx_dsp_create_scb(chip,"AsynCodecInputSCB",(u32 *)&async_codec_input_scb, HFG_TREE_SCB);
1656		if (snd_BUG_ON(!async_codec_scb_desc))
1657			return -EIO;
1658
1659		async_codec_scb_desc->parent_scb_ptr = NULL;
1660		async_codec_scb_desc->next_scb_ptr = spdifi_scb_desc;
1661		async_codec_scb_desc->sub_list_ptr = ins->the_null_scb;
1662		async_codec_scb_desc->task_entry = s16_async_codec_input_task;
1663
1664		spdifi_scb_desc->parent_scb_ptr = async_codec_scb_desc;
1665		spdifi_scb_desc->next_scb_ptr = spdifo_scb_desc;
1666		spdifi_scb_desc->sub_list_ptr = ins->the_null_scb;
1667		spdifi_scb_desc->task_entry = spdifi_task;
1668
1669		spdifo_scb_desc->parent_scb_ptr = spdifi_scb_desc;
1670		spdifo_scb_desc->next_scb_ptr = fg_entry;
1671		spdifo_scb_desc->sub_list_ptr = ins->the_null_scb;
1672		spdifo_scb_desc->task_entry = spdifo_task;
1673
1674		/* this one is faked, as the parnet of SPDIFO task
1675		   is the FG task tree */
1676		fg_entry->parent_scb_ptr = spdifo_scb_desc;
1677
1678		/* for proc fs */
1679		cs46xx_dsp_proc_register_scb_desc (chip,spdifo_scb_desc);
1680		cs46xx_dsp_proc_register_scb_desc (chip,spdifi_scb_desc);
1681		cs46xx_dsp_proc_register_scb_desc (chip,async_codec_scb_desc);
1682
1683		/* Async MASTER ENABLE, affects both SPDIF input and output */
1684		snd_cs46xx_pokeBA0(chip, BA0_ASER_MASTER, 0x1 );
1685	}
1686
1687	return 0;
1688}
1689
1690static void cs46xx_dsp_disable_spdif_hw (struct snd_cs46xx *chip)
1691{
1692	struct dsp_spos_instance * ins = chip->dsp_spos_instance;
1693
1694	/* set SPDIF output FIFO slot */
1695	snd_cs46xx_pokeBA0(chip, BA0_ASER_FADDR, 0);
1696
1697	/* SPDIF output MASTER ENABLE */
1698	cs46xx_poke_via_dsp (chip,SP_SPDOUT_CONTROL, 0);
1699
1700	/* right and left validate bit */
1701	/*cs46xx_poke_via_dsp (chip,SP_SPDOUT_CSUV, ins->spdif_csuv_default);*/
1702	cs46xx_poke_via_dsp (chip,SP_SPDOUT_CSUV, 0x0);
1703
1704	/* clear fifo pointer */
1705	cs46xx_poke_via_dsp (chip,SP_SPDIN_FIFOPTR, 0x0);
1706
1707	/* monitor state */
1708	ins->spdif_status_out &= ~DSP_SPDIF_STATUS_HW_ENABLED;
1709}
1710
1711int cs46xx_dsp_enable_spdif_hw (struct snd_cs46xx *chip)
1712{
1713	struct dsp_spos_instance * ins = chip->dsp_spos_instance;
1714
1715	/* if hw-ctrl already enabled, turn off to reset logic ... */
1716	cs46xx_dsp_disable_spdif_hw (chip);
1717	udelay(50);
1718
1719	/* set SPDIF output FIFO slot */
1720	snd_cs46xx_pokeBA0(chip, BA0_ASER_FADDR, ( 0x8000 | ((SP_SPDOUT_FIFO >> 4) << 4) ));
1721
1722	/* SPDIF output MASTER ENABLE */
1723	cs46xx_poke_via_dsp (chip,SP_SPDOUT_CONTROL, 0x80000000);
1724
1725	/* right and left validate bit */
1726	cs46xx_poke_via_dsp (chip,SP_SPDOUT_CSUV, ins->spdif_csuv_default);
1727
1728	/* monitor state */
1729	ins->spdif_status_out |= DSP_SPDIF_STATUS_HW_ENABLED;
1730
1731	return 0;
1732}
1733
1734int cs46xx_dsp_enable_spdif_in (struct snd_cs46xx *chip)
1735{
1736	struct dsp_spos_instance * ins = chip->dsp_spos_instance;
1737
1738	/* turn on amplifier */
1739	chip->active_ctrl(chip, 1);
1740	chip->amplifier_ctrl(chip, 1);
1741
1742	if (snd_BUG_ON(ins->asynch_rx_scb))
1743		return -EINVAL;
1744	if (snd_BUG_ON(!ins->spdif_in_src))
1745		return -EINVAL;
1746
1747	mutex_lock(&chip->spos_mutex);
1748
1749	if ( ! (ins->spdif_status_out & DSP_SPDIF_STATUS_INPUT_CTRL_ENABLED) ) {
1750		/* time countdown enable */
1751		cs46xx_poke_via_dsp (chip,SP_ASER_COUNTDOWN, 0x80000005);
1752		/* NOTE: 80000005 value is just magic. With all values
1753		   that I've tested this one seem to give the best result.
1754		   Got no explication why. (Benny) */
1755
1756		/* SPDIF input MASTER ENABLE */
1757		cs46xx_poke_via_dsp (chip,SP_SPDIN_CONTROL, 0x800003ff);
1758
1759		ins->spdif_status_out |= DSP_SPDIF_STATUS_INPUT_CTRL_ENABLED;
1760	}
1761
1762	/* create and start the asynchronous receiver SCB */
1763	ins->asynch_rx_scb = cs46xx_dsp_create_asynch_fg_rx_scb(chip,"AsynchFGRxSCB",
1764								ASYNCRX_SCB_ADDR,
1765								SPDIFI_SCB_INST,
1766								SPDIFI_IP_OUTPUT_BUFFER1,
1767								ins->spdif_in_src,
1768								SCB_ON_PARENT_SUBLIST_SCB);
1769
1770	spin_lock_irq(&chip->reg_lock);
1771
1772	/* reset SPDIF input sample buffer pointer */
1773	/*snd_cs46xx_poke (chip, (SPDIFI_SCB_INST + 0x0c) << 2,
1774	  (SPDIFI_IP_OUTPUT_BUFFER1 << 0x10) | 0xFFFC);*/
1775
1776	/* reset FIFO ptr */
1777	/*cs46xx_poke_via_dsp (chip,SP_SPDIN_FIFOPTR, 0x0);*/
1778	cs46xx_src_link(chip,ins->spdif_in_src);
1779
1780	/* unmute SRC volume */
1781	cs46xx_dsp_scb_set_volume (chip,ins->spdif_in_src,0x7fff,0x7fff);
1782
1783	spin_unlock_irq(&chip->reg_lock);
1784
1785	/* set SPDIF input sample rate and unmute
1786	   NOTE: only 48khz support for SPDIF input this time */
1787	/* cs46xx_dsp_set_src_sample_rate(chip,ins->spdif_in_src,48000); */
1788
1789	/* monitor state */
1790	ins->spdif_status_in = 1;
1791	mutex_unlock(&chip->spos_mutex);
1792
1793	return 0;
1794}
1795
1796int cs46xx_dsp_disable_spdif_in (struct snd_cs46xx *chip)
1797{
1798	struct dsp_spos_instance * ins = chip->dsp_spos_instance;
1799
1800	if (snd_BUG_ON(!ins->asynch_rx_scb))
1801		return -EINVAL;
1802	if (snd_BUG_ON(!ins->spdif_in_src))
1803		return -EINVAL;
1804
1805	mutex_lock(&chip->spos_mutex);
1806
1807	/* Remove the asynchronous receiver SCB */
1808	cs46xx_dsp_remove_scb (chip,ins->asynch_rx_scb);
1809	ins->asynch_rx_scb = NULL;
1810
1811	cs46xx_src_unlink(chip,ins->spdif_in_src);
1812
1813	/* monitor state */
1814	ins->spdif_status_in = 0;
1815	mutex_unlock(&chip->spos_mutex);
1816
1817	/* restore amplifier */
1818	chip->active_ctrl(chip, -1);
1819	chip->amplifier_ctrl(chip, -1);
1820
1821	return 0;
1822}
1823
1824int cs46xx_dsp_enable_pcm_capture (struct snd_cs46xx *chip)
1825{
1826	struct dsp_spos_instance * ins = chip->dsp_spos_instance;
1827
1828	if (snd_BUG_ON(ins->pcm_input))
1829		return -EINVAL;
1830	if (snd_BUG_ON(!ins->ref_snoop_scb))
1831		return -EINVAL;
1832
1833	mutex_lock(&chip->spos_mutex);
1834	ins->pcm_input = cs46xx_add_record_source(chip,ins->ref_snoop_scb,PCMSERIALIN_PCM_SCB_ADDR,
1835                                                  "PCMSerialInput_Wave");
1836	mutex_unlock(&chip->spos_mutex);
1837
1838	return 0;
1839}
1840
1841int cs46xx_dsp_disable_pcm_capture (struct snd_cs46xx *chip)
1842{
1843	struct dsp_spos_instance * ins = chip->dsp_spos_instance;
1844
1845	if (snd_BUG_ON(!ins->pcm_input))
1846		return -EINVAL;
1847
1848	mutex_lock(&chip->spos_mutex);
1849	cs46xx_dsp_remove_scb (chip,ins->pcm_input);
1850	ins->pcm_input = NULL;
1851	mutex_unlock(&chip->spos_mutex);
1852
1853	return 0;
1854}
1855
1856int cs46xx_dsp_enable_adc_capture (struct snd_cs46xx *chip)
1857{
1858	struct dsp_spos_instance * ins = chip->dsp_spos_instance;
1859
1860	if (snd_BUG_ON(ins->adc_input))
1861		return -EINVAL;
1862	if (snd_BUG_ON(!ins->codec_in_scb))
1863		return -EINVAL;
1864
1865	mutex_lock(&chip->spos_mutex);
1866	ins->adc_input = cs46xx_add_record_source(chip,ins->codec_in_scb,PCMSERIALIN_SCB_ADDR,
1867						  "PCMSerialInput_ADC");
1868	mutex_unlock(&chip->spos_mutex);
1869
1870	return 0;
1871}
1872
1873int cs46xx_dsp_disable_adc_capture (struct snd_cs46xx *chip)
1874{
1875	struct dsp_spos_instance * ins = chip->dsp_spos_instance;
1876
1877	if (snd_BUG_ON(!ins->adc_input))
1878		return -EINVAL;
1879
1880	mutex_lock(&chip->spos_mutex);
1881	cs46xx_dsp_remove_scb (chip,ins->adc_input);
1882	ins->adc_input = NULL;
1883	mutex_unlock(&chip->spos_mutex);
1884
1885	return 0;
1886}
1887
1888int cs46xx_poke_via_dsp (struct snd_cs46xx *chip, u32 address, u32 data)
1889{
1890	u32 temp;
1891	int  i;
1892
1893	/* santiy check the parameters.  (These numbers are not 100% correct.  They are
1894	   a rough guess from looking at the controller spec.) */
1895	if (address < 0x8000 || address >= 0x9000)
1896		return -EINVAL;
1897        
1898	/* initialize the SP_IO_WRITE SCB with the data. */
1899	temp = ( address << 16 ) | ( address & 0x0000FFFF);   /* offset 0 <-- address2 : address1 */
1900
1901	snd_cs46xx_poke(chip,( SPIOWRITE_SCB_ADDR      << 2), temp);
1902	snd_cs46xx_poke(chip,((SPIOWRITE_SCB_ADDR + 1) << 2), data); /* offset 1 <-- data1 */
1903	snd_cs46xx_poke(chip,((SPIOWRITE_SCB_ADDR + 2) << 2), data); /* offset 1 <-- data2 */
1904    
1905	/* Poke this location to tell the task to start */
1906	snd_cs46xx_poke(chip,((SPIOWRITE_SCB_ADDR + 6) << 2), SPIOWRITE_SCB_ADDR << 0x10);
1907
1908	/* Verify that the task ran */
1909	for (i=0; i<25; i++) {
1910		udelay(125);
1911
1912		temp =  snd_cs46xx_peek(chip,((SPIOWRITE_SCB_ADDR + 6) << 2));
1913		if (temp == 0x00000000)
1914			break;
1915	}
1916
1917	if (i == 25) {
1918		dev_err(chip->card->dev,
1919			"dsp_spos: SPIOWriteTask not responding\n");
1920		return -EBUSY;
1921	}
1922
1923	return 0;
1924}
1925
1926int cs46xx_dsp_set_dac_volume (struct snd_cs46xx * chip, u16 left, u16 right)
1927{
1928	struct dsp_spos_instance * ins = chip->dsp_spos_instance;
1929	struct dsp_scb_descriptor * scb; 
1930
1931	mutex_lock(&chip->spos_mutex);
1932	
1933	/* main output */
1934	scb = ins->master_mix_scb->sub_list_ptr;
1935	while (scb != ins->the_null_scb) {
1936		cs46xx_dsp_scb_set_volume (chip,scb,left,right);
1937		scb = scb->next_scb_ptr;
1938	}
1939
1940	/* rear output */
1941	scb = ins->rear_mix_scb->sub_list_ptr;
1942	while (scb != ins->the_null_scb) {
1943		cs46xx_dsp_scb_set_volume (chip,scb,left,right);
1944		scb = scb->next_scb_ptr;
1945	}
1946
1947	ins->dac_volume_left = left;
1948	ins->dac_volume_right = right;
1949
1950	mutex_unlock(&chip->spos_mutex);
1951
1952	return 0;
1953}
1954
1955int cs46xx_dsp_set_iec958_volume (struct snd_cs46xx * chip, u16 left, u16 right)
1956{
1957	struct dsp_spos_instance * ins = chip->dsp_spos_instance;
1958
1959	mutex_lock(&chip->spos_mutex);
1960
1961	if (ins->asynch_rx_scb != NULL)
1962		cs46xx_dsp_scb_set_volume (chip,ins->asynch_rx_scb,
1963					   left,right);
1964
1965	ins->spdif_input_volume_left = left;
1966	ins->spdif_input_volume_right = right;
1967
1968	mutex_unlock(&chip->spos_mutex);
1969
1970	return 0;
1971}
1972
1973#ifdef CONFIG_PM_SLEEP
1974int cs46xx_dsp_resume(struct snd_cs46xx * chip)
1975{
1976	struct dsp_spos_instance * ins = chip->dsp_spos_instance;
1977	int i, err;
1978
1979	/* clear parameter, sample and code areas */
1980	snd_cs46xx_clear_BA1(chip, DSP_PARAMETER_BYTE_OFFSET,
1981			     DSP_PARAMETER_BYTE_SIZE);
1982	snd_cs46xx_clear_BA1(chip, DSP_SAMPLE_BYTE_OFFSET,
1983			     DSP_SAMPLE_BYTE_SIZE);
1984	snd_cs46xx_clear_BA1(chip, DSP_CODE_BYTE_OFFSET, DSP_CODE_BYTE_SIZE);
1985
1986	for (i = 0; i < ins->nmodules; i++) {
1987		struct dsp_module_desc *module = &ins->modules[i];
1988		struct dsp_segment_desc *seg;
1989		u32 doffset, dsize;
1990
1991		seg = get_segment_desc(module, SEGTYPE_SP_PARAMETER);
1992		err = dsp_load_parameter(chip, seg);
1993		if (err < 0)
1994			return err;
1995
1996		seg = get_segment_desc(module, SEGTYPE_SP_SAMPLE);
1997		err = dsp_load_sample(chip, seg);
1998		if (err < 0)
1999			return err;
2000
2001		seg = get_segment_desc(module, SEGTYPE_SP_PROGRAM);
2002		if (!seg)
2003			continue;
2004
2005		doffset = seg->offset * 4 + module->load_address * 4
2006			+ DSP_CODE_BYTE_OFFSET;
2007		dsize   = seg->size * 4;
2008		err = snd_cs46xx_download(chip,
2009					  ins->code.data + module->load_address,
2010					  doffset, dsize);
2011		if (err < 0)
2012			return err;
2013	}
2014
2015	for (i = 0; i < ins->ntask; i++) {
2016		struct dsp_task_descriptor *t = &ins->tasks[i];
2017		_dsp_create_task_tree(chip, t->data, t->address, t->size);
2018	}
2019
2020	for (i = 0; i < ins->nscb; i++) {
2021		struct dsp_scb_descriptor *s = &ins->scbs[i];
2022		if (s->deleted)
2023			continue;
2024		_dsp_create_scb(chip, s->data, s->address);
2025	}
2026	for (i = 0; i < ins->nscb; i++) {
2027		struct dsp_scb_descriptor *s = &ins->scbs[i];
2028		if (s->deleted)
2029			continue;
2030		if (s->updated)
2031			cs46xx_dsp_spos_update_scb(chip, s);
2032		if (s->volume_set)
2033			cs46xx_dsp_scb_set_volume(chip, s,
2034						  s->volume[0], s->volume[1]);
2035	}
2036	if (ins->spdif_status_out & DSP_SPDIF_STATUS_HW_ENABLED) {
2037		cs46xx_dsp_enable_spdif_hw(chip);
2038		snd_cs46xx_poke(chip, (ins->ref_snoop_scb->address + 2) << 2,
2039				(OUTPUT_SNOOP_BUFFER + 0x10) << 0x10);
2040		if (ins->spdif_status_out & DSP_SPDIF_STATUS_PLAYBACK_OPEN)
2041			cs46xx_poke_via_dsp(chip, SP_SPDOUT_CSUV,
2042					    ins->spdif_csuv_stream);
2043	}
2044	if (chip->dsp_spos_instance->spdif_status_in) {
2045		cs46xx_poke_via_dsp(chip, SP_ASER_COUNTDOWN, 0x80000005);
2046		cs46xx_poke_via_dsp(chip, SP_SPDIN_CONTROL, 0x800003ff);
2047	}
2048	return 0;
2049}
2050#endif
v6.8
   1// SPDX-License-Identifier: GPL-2.0-or-later
   2/*
 
 
 
 
 
 
 
 
 
 
 
 
 
 
   3 */
   4
   5/*
   6 * 2002-07 Benny Sjostrand benny@hostmobility.com
   7 */
   8
   9
  10#include <linux/io.h>
  11#include <linux/delay.h>
  12#include <linux/pm.h>
  13#include <linux/init.h>
  14#include <linux/slab.h>
  15#include <linux/vmalloc.h>
  16#include <linux/mutex.h>
  17
  18#include <sound/core.h>
  19#include <sound/control.h>
  20#include <sound/info.h>
  21#include <sound/asoundef.h>
  22#include "cs46xx.h"
  23
  24#include "cs46xx_lib.h"
  25#include "dsp_spos.h"
  26
  27static int cs46xx_dsp_async_init (struct snd_cs46xx *chip,
  28				  struct dsp_scb_descriptor * fg_entry);
  29
  30static const enum wide_opcode wide_opcodes[] = {
  31	WIDE_FOR_BEGIN_LOOP,
  32	WIDE_FOR_BEGIN_LOOP2,
  33	WIDE_COND_GOTO_ADDR,
  34	WIDE_COND_GOTO_CALL,
  35	WIDE_TBEQ_COND_GOTO_ADDR,
  36	WIDE_TBEQ_COND_CALL_ADDR,
  37	WIDE_TBEQ_NCOND_GOTO_ADDR,
  38	WIDE_TBEQ_NCOND_CALL_ADDR,
  39	WIDE_TBEQ_COND_GOTO1_ADDR,
  40	WIDE_TBEQ_COND_CALL1_ADDR,
  41	WIDE_TBEQ_NCOND_GOTOI_ADDR,
  42	WIDE_TBEQ_NCOND_CALL1_ADDR
  43};
  44
  45static int shadow_and_reallocate_code (struct snd_cs46xx * chip, u32 * data, u32 size,
  46				       u32 overlay_begin_address)
  47{
  48	unsigned int i = 0, j, nreallocated = 0;
  49	u32 hival,loval,address;
  50	u32 mop_operands,mop_type,wide_op;
  51	struct dsp_spos_instance * ins = chip->dsp_spos_instance;
  52
  53	if (snd_BUG_ON(size %2))
  54		return -EINVAL;
  55  
  56	while (i < size) {
  57		loval = data[i++];
  58		hival = data[i++];
  59
  60		if (ins->code.offset > 0) {
  61			mop_operands = (hival >> 6) & 0x03fff;
  62			mop_type = mop_operands >> 10;
  63      
  64			/* check for wide type instruction */
  65			if (mop_type == 0 &&
  66			    (mop_operands & WIDE_LADD_INSTR_MASK) == 0 &&
  67			    (mop_operands & WIDE_INSTR_MASK) != 0) {
  68				wide_op = loval & 0x7f;
  69				for (j = 0;j < ARRAY_SIZE(wide_opcodes); ++j) {
  70					if (wide_opcodes[j] == wide_op) {
  71						/* need to reallocate instruction */
  72						address  = (hival & 0x00FFF) << 5;
  73						address |=  loval >> 15;
  74            
  75						dev_dbg(chip->card->dev,
  76							"handle_wideop[1]: %05x:%05x addr %04x\n",
  77							hival, loval, address);
  78            
  79						if ( !(address & 0x8000) ) {
  80							address += (ins->code.offset / 2) - overlay_begin_address;
  81						} else {
  82							dev_dbg(chip->card->dev,
  83								"handle_wideop[1]: ROM symbol not reallocated\n");
  84						}
  85            
  86						hival &= 0xFF000;
  87						loval &= 0x07FFF;
  88            
  89						hival |= ( (address >> 5)  & 0x00FFF);
  90						loval |= ( (address << 15) & 0xF8000);
  91            
  92						address  = (hival & 0x00FFF) << 5;
  93						address |=  loval >> 15;
  94            
  95						dev_dbg(chip->card->dev,
  96							"handle_wideop:[2] %05x:%05x addr %04x\n",
  97							hival, loval, address);
  98						nreallocated++;
  99					} /* wide_opcodes[j] == wide_op */
 100				} /* for */
 101			} /* mod_type == 0 ... */
 102		} /* ins->code.offset > 0 */
 103
 104		ins->code.data[ins->code.size++] = loval;
 105		ins->code.data[ins->code.size++] = hival;
 106	}
 107
 108	dev_dbg(chip->card->dev,
 109		"dsp_spos: %d instructions reallocated\n", nreallocated);
 110	return nreallocated;
 111}
 112
 113static struct dsp_segment_desc * get_segment_desc (struct dsp_module_desc * module, int seg_type)
 114{
 115	int i;
 116	for (i = 0;i < module->nsegments; ++i) {
 117		if (module->segments[i].segment_type == seg_type) {
 118			return (module->segments + i);
 119		}
 120	}
 121
 122	return NULL;
 123};
 124
 125static int find_free_symbol_index (struct dsp_spos_instance * ins)
 126{
 127	int index = ins->symbol_table.nsymbols,i;
 128
 129	for (i = ins->symbol_table.highest_frag_index; i < ins->symbol_table.nsymbols; ++i) {
 130		if (ins->symbol_table.symbols[i].deleted) {
 131			index = i;
 132			break;
 133		}
 134	}
 135
 136	return index;
 137}
 138
 139static int add_symbols (struct snd_cs46xx * chip, struct dsp_module_desc * module)
 140{
 141	int i;
 142	struct dsp_spos_instance * ins = chip->dsp_spos_instance;
 143
 144	if (module->symbol_table.nsymbols > 0) {
 145		if (!strcmp(module->symbol_table.symbols[0].symbol_name, "OVERLAYBEGINADDRESS") &&
 146		    module->symbol_table.symbols[0].symbol_type == SYMBOL_CONSTANT ) {
 147			module->overlay_begin_address = module->symbol_table.symbols[0].address;
 148		}
 149	}
 150
 151	for (i = 0;i < module->symbol_table.nsymbols; ++i) {
 152		if (ins->symbol_table.nsymbols == (DSP_MAX_SYMBOLS - 1)) {
 153			dev_err(chip->card->dev,
 154				"dsp_spos: symbol table is full\n");
 155			return -ENOMEM;
 156		}
 157
 158
 159		if (cs46xx_dsp_lookup_symbol(chip,
 160					     module->symbol_table.symbols[i].symbol_name,
 161					     module->symbol_table.symbols[i].symbol_type) == NULL) {
 162
 163			ins->symbol_table.symbols[ins->symbol_table.nsymbols] = module->symbol_table.symbols[i];
 164			ins->symbol_table.symbols[ins->symbol_table.nsymbols].address += ((ins->code.offset / 2) - module->overlay_begin_address);
 165			ins->symbol_table.symbols[ins->symbol_table.nsymbols].module = module;
 166			ins->symbol_table.symbols[ins->symbol_table.nsymbols].deleted = 0;
 167
 168			if (ins->symbol_table.nsymbols > ins->symbol_table.highest_frag_index) 
 169				ins->symbol_table.highest_frag_index = ins->symbol_table.nsymbols;
 170
 171			ins->symbol_table.nsymbols++;
 172		} else {
 173#if 0
 174			dev_dbg(chip->card->dev,
 175				"dsp_spos: symbol <%s> duplicated, probably nothing wrong with that (Cirrus?)\n",
 176				module->symbol_table.symbols[i].symbol_name); */
 177#endif
 178		}
 179	}
 180
 181	return 0;
 182}
 183
 184static struct dsp_symbol_entry *
 185add_symbol (struct snd_cs46xx * chip, char * symbol_name, u32 address, int type)
 186{
 187	struct dsp_spos_instance * ins = chip->dsp_spos_instance;
 188	struct dsp_symbol_entry * symbol = NULL;
 189	int index;
 190
 191	if (ins->symbol_table.nsymbols == (DSP_MAX_SYMBOLS - 1)) {
 192		dev_err(chip->card->dev, "dsp_spos: symbol table is full\n");
 193		return NULL;
 194	}
 195  
 196	if (cs46xx_dsp_lookup_symbol(chip,
 197				     symbol_name,
 198				     type) != NULL) {
 199		dev_err(chip->card->dev,
 200			"dsp_spos: symbol <%s> duplicated\n", symbol_name);
 201		return NULL;
 202	}
 203
 204	index = find_free_symbol_index (ins);
 205
 206	strcpy (ins->symbol_table.symbols[index].symbol_name, symbol_name);
 207	ins->symbol_table.symbols[index].address = address;
 208	ins->symbol_table.symbols[index].symbol_type = type;
 209	ins->symbol_table.symbols[index].module = NULL;
 210	ins->symbol_table.symbols[index].deleted = 0;
 211	symbol = (ins->symbol_table.symbols + index);
 212
 213	if (index > ins->symbol_table.highest_frag_index) 
 214		ins->symbol_table.highest_frag_index = index;
 215
 216	if (index == ins->symbol_table.nsymbols)
 217		ins->symbol_table.nsymbols++; /* no frag. in list */
 218
 219	return symbol;
 220}
 221
 222struct dsp_spos_instance *cs46xx_dsp_spos_create (struct snd_cs46xx * chip)
 223{
 224	struct dsp_spos_instance * ins = kzalloc(sizeof(struct dsp_spos_instance), GFP_KERNEL);
 225
 226	if (ins == NULL)
 227		return NULL;
 228
 229	/* better to use vmalloc for this big table */
 230	ins->symbol_table.symbols =
 231		vmalloc(array_size(DSP_MAX_SYMBOLS,
 232				   sizeof(struct dsp_symbol_entry)));
 233	ins->code.data = kmalloc(DSP_CODE_BYTE_SIZE, GFP_KERNEL);
 234	ins->modules = kmalloc_array(DSP_MAX_MODULES,
 235				     sizeof(struct dsp_module_desc),
 236				     GFP_KERNEL);
 237	if (!ins->symbol_table.symbols || !ins->code.data || !ins->modules) {
 238		cs46xx_dsp_spos_destroy(chip);
 239		goto error;
 240	}
 241	ins->symbol_table.nsymbols = 0;
 242	ins->symbol_table.highest_frag_index = 0;
 243	ins->code.offset = 0;
 244	ins->code.size = 0;
 245	ins->nscb = 0;
 246	ins->ntask = 0;
 247	ins->nmodules = 0;
 248
 249	/* default SPDIF input sample rate
 250	   to 48000 khz */
 251	ins->spdif_in_sample_rate = 48000;
 252
 253	/* maximize volume */
 254	ins->dac_volume_right = 0x8000;
 255	ins->dac_volume_left = 0x8000;
 256	ins->spdif_input_volume_right = 0x8000;
 257	ins->spdif_input_volume_left = 0x8000;
 258
 259	/* set left and right validity bits and
 260	   default channel status */
 261	ins->spdif_csuv_default =
 262		ins->spdif_csuv_stream =
 263	 /* byte 0 */  ((unsigned int)_wrap_all_bits(  (SNDRV_PCM_DEFAULT_CON_SPDIF        & 0xff)) << 24) |
 264	 /* byte 1 */  ((unsigned int)_wrap_all_bits( ((SNDRV_PCM_DEFAULT_CON_SPDIF >> 8) & 0xff)) << 16) |
 265	 /* byte 3 */   (unsigned int)_wrap_all_bits(  (SNDRV_PCM_DEFAULT_CON_SPDIF >> 24) & 0xff) |
 266	 /* left and right validity bits */ (1 << 13) | (1 << 12);
 267
 268	return ins;
 269
 270error:
 271	kfree(ins->modules);
 272	kfree(ins->code.data);
 273	vfree(ins->symbol_table.symbols);
 274	kfree(ins);
 275	return NULL;
 276}
 277
 278void  cs46xx_dsp_spos_destroy (struct snd_cs46xx * chip)
 279{
 280	int i;
 281	struct dsp_spos_instance * ins = chip->dsp_spos_instance;
 282
 283	if (snd_BUG_ON(!ins))
 284		return;
 285
 286	mutex_lock(&chip->spos_mutex);
 287	for (i = 0; i < ins->nscb; ++i) {
 288		if (ins->scbs[i].deleted) continue;
 289
 290		cs46xx_dsp_proc_free_scb_desc ( (ins->scbs + i) );
 291#ifdef CONFIG_PM_SLEEP
 292		kfree(ins->scbs[i].data);
 293#endif
 294	}
 295
 296	kfree(ins->code.data);
 297	vfree(ins->symbol_table.symbols);
 298	kfree(ins->modules);
 299	kfree(ins);
 300	mutex_unlock(&chip->spos_mutex);
 301}
 302
 303static int dsp_load_parameter(struct snd_cs46xx *chip,
 304			      struct dsp_segment_desc *parameter)
 305{
 306	u32 doffset, dsize;
 307
 308	if (!parameter) {
 309		dev_dbg(chip->card->dev,
 310			"dsp_spos: module got no parameter segment\n");
 311		return 0;
 312	}
 313
 314	doffset = (parameter->offset * 4 + DSP_PARAMETER_BYTE_OFFSET);
 315	dsize   = parameter->size * 4;
 316
 317	dev_dbg(chip->card->dev,
 318		"dsp_spos: downloading parameter data to chip (%08x-%08x)\n",
 319		    doffset,doffset + dsize);
 320	if (snd_cs46xx_download (chip, parameter->data, doffset, dsize)) {
 321		dev_err(chip->card->dev,
 322			"dsp_spos: failed to download parameter data to DSP\n");
 323		return -EINVAL;
 324	}
 325	return 0;
 326}
 327
 328static int dsp_load_sample(struct snd_cs46xx *chip,
 329			   struct dsp_segment_desc *sample)
 330{
 331	u32 doffset, dsize;
 332
 333	if (!sample) {
 334		dev_dbg(chip->card->dev,
 335			"dsp_spos: module got no sample segment\n");
 336		return 0;
 337	}
 338
 339	doffset = (sample->offset * 4  + DSP_SAMPLE_BYTE_OFFSET);
 340	dsize   =  sample->size * 4;
 341
 342	dev_dbg(chip->card->dev,
 343		"dsp_spos: downloading sample data to chip (%08x-%08x)\n",
 344		    doffset,doffset + dsize);
 345
 346	if (snd_cs46xx_download (chip,sample->data,doffset,dsize)) {
 347		dev_err(chip->card->dev,
 348			"dsp_spos: failed to sample data to DSP\n");
 349		return -EINVAL;
 350	}
 351	return 0;
 352}
 353
 354int cs46xx_dsp_load_module (struct snd_cs46xx * chip, struct dsp_module_desc * module)
 355{
 356	struct dsp_spos_instance * ins = chip->dsp_spos_instance;
 357	struct dsp_segment_desc * code = get_segment_desc (module,SEGTYPE_SP_PROGRAM);
 358	u32 doffset, dsize;
 359	int err;
 360
 361	if (ins->nmodules == DSP_MAX_MODULES - 1) {
 362		dev_err(chip->card->dev,
 363			"dsp_spos: to many modules loaded into DSP\n");
 364		return -ENOMEM;
 365	}
 366
 367	dev_dbg(chip->card->dev,
 368		"dsp_spos: loading module %s into DSP\n", module->module_name);
 369  
 370	if (ins->nmodules == 0) {
 371		dev_dbg(chip->card->dev, "dsp_spos: clearing parameter area\n");
 372		snd_cs46xx_clear_BA1(chip, DSP_PARAMETER_BYTE_OFFSET, DSP_PARAMETER_BYTE_SIZE);
 373	}
 374  
 375	err = dsp_load_parameter(chip, get_segment_desc(module,
 376							SEGTYPE_SP_PARAMETER));
 377	if (err < 0)
 378		return err;
 379
 380	if (ins->nmodules == 0) {
 381		dev_dbg(chip->card->dev, "dsp_spos: clearing sample area\n");
 382		snd_cs46xx_clear_BA1(chip, DSP_SAMPLE_BYTE_OFFSET, DSP_SAMPLE_BYTE_SIZE);
 383	}
 384
 385	err = dsp_load_sample(chip, get_segment_desc(module,
 386						     SEGTYPE_SP_SAMPLE));
 387	if (err < 0)
 388		return err;
 389
 390	if (ins->nmodules == 0) {
 391		dev_dbg(chip->card->dev, "dsp_spos: clearing code area\n");
 392		snd_cs46xx_clear_BA1(chip, DSP_CODE_BYTE_OFFSET, DSP_CODE_BYTE_SIZE);
 393	}
 394
 395	if (code == NULL) {
 396		dev_dbg(chip->card->dev,
 397			"dsp_spos: module got no code segment\n");
 398	} else {
 399		if (ins->code.offset + code->size > DSP_CODE_BYTE_SIZE) {
 400			dev_err(chip->card->dev,
 401				"dsp_spos: no space available in DSP\n");
 402			return -ENOMEM;
 403		}
 404
 405		module->load_address = ins->code.offset;
 406		module->overlay_begin_address = 0x000;
 407
 408		/* if module has a code segment it must have
 409		   symbol table */
 410		if (snd_BUG_ON(!module->symbol_table.symbols))
 411			return -ENOMEM;
 412		if (add_symbols(chip,module)) {
 413			dev_err(chip->card->dev,
 414				"dsp_spos: failed to load symbol table\n");
 415			return -ENOMEM;
 416		}
 417    
 418		doffset = (code->offset * 4 + ins->code.offset * 4 + DSP_CODE_BYTE_OFFSET);
 419		dsize   = code->size * 4;
 420		dev_dbg(chip->card->dev,
 421			"dsp_spos: downloading code to chip (%08x-%08x)\n",
 422			    doffset,doffset + dsize);   
 423
 424		module->nfixups = shadow_and_reallocate_code(chip,code->data,code->size,module->overlay_begin_address);
 425
 426		if (snd_cs46xx_download (chip,(ins->code.data + ins->code.offset),doffset,dsize)) {
 427			dev_err(chip->card->dev,
 428				"dsp_spos: failed to download code to DSP\n");
 429			return -EINVAL;
 430		}
 431
 432		ins->code.offset += code->size;
 433	}
 434
 435	/* NOTE: module segments and symbol table must be
 436	   statically allocated. Case that module data is
 437	   not generated by the ospparser */
 438	ins->modules[ins->nmodules] = *module;
 439	ins->nmodules++;
 440
 441	return 0;
 442}
 443
 444struct dsp_symbol_entry *
 445cs46xx_dsp_lookup_symbol (struct snd_cs46xx * chip, char * symbol_name, int symbol_type)
 446{
 447	int i;
 448	struct dsp_spos_instance * ins = chip->dsp_spos_instance;
 449
 450	for ( i = 0; i < ins->symbol_table.nsymbols; ++i ) {
 451
 452		if (ins->symbol_table.symbols[i].deleted)
 453			continue;
 454
 455		if (!strcmp(ins->symbol_table.symbols[i].symbol_name,symbol_name) &&
 456		    ins->symbol_table.symbols[i].symbol_type == symbol_type) {
 457			return (ins->symbol_table.symbols + i);
 458		}
 459	}
 460
 461#if 0
 462	dev_err(chip->card->dev, "dsp_spos: symbol <%s> type %02x not found\n",
 463		symbol_name,symbol_type);
 464#endif
 465
 466	return NULL;
 467}
 468
 469
 470#ifdef CONFIG_SND_PROC_FS
 471static struct dsp_symbol_entry *
 472cs46xx_dsp_lookup_symbol_addr (struct snd_cs46xx * chip, u32 address, int symbol_type)
 473{
 474	int i;
 475	struct dsp_spos_instance * ins = chip->dsp_spos_instance;
 476
 477	for ( i = 0; i < ins->symbol_table.nsymbols; ++i ) {
 478
 479		if (ins->symbol_table.symbols[i].deleted)
 480			continue;
 481
 482		if (ins->symbol_table.symbols[i].address == address &&
 483		    ins->symbol_table.symbols[i].symbol_type == symbol_type) {
 484			return (ins->symbol_table.symbols + i);
 485		}
 486	}
 487
 488
 489	return NULL;
 490}
 491
 492
 493static void cs46xx_dsp_proc_symbol_table_read (struct snd_info_entry *entry,
 494					       struct snd_info_buffer *buffer)
 495{
 496	struct snd_cs46xx *chip = entry->private_data;
 497	struct dsp_spos_instance * ins = chip->dsp_spos_instance;
 498	int i;
 499
 500	snd_iprintf(buffer, "SYMBOLS:\n");
 501	for ( i = 0; i < ins->symbol_table.nsymbols; ++i ) {
 502		char *module_str = "system";
 503
 504		if (ins->symbol_table.symbols[i].deleted)
 505			continue;
 506
 507		if (ins->symbol_table.symbols[i].module != NULL) {
 508			module_str = ins->symbol_table.symbols[i].module->module_name;
 509		}
 510
 511    
 512		snd_iprintf(buffer, "%04X <%02X> %s [%s]\n",
 513			    ins->symbol_table.symbols[i].address,
 514			    ins->symbol_table.symbols[i].symbol_type,
 515			    ins->symbol_table.symbols[i].symbol_name,
 516			    module_str);    
 517	}
 518}
 519
 520
 521static void cs46xx_dsp_proc_modules_read (struct snd_info_entry *entry,
 522					  struct snd_info_buffer *buffer)
 523{
 524	struct snd_cs46xx *chip = entry->private_data;
 525	struct dsp_spos_instance * ins = chip->dsp_spos_instance;
 526	int i,j;
 527
 528	mutex_lock(&chip->spos_mutex);
 529	snd_iprintf(buffer, "MODULES:\n");
 530	for ( i = 0; i < ins->nmodules; ++i ) {
 531		snd_iprintf(buffer, "\n%s:\n", ins->modules[i].module_name);
 532		snd_iprintf(buffer, "   %d symbols\n", ins->modules[i].symbol_table.nsymbols);
 533		snd_iprintf(buffer, "   %d fixups\n", ins->modules[i].nfixups);
 534
 535		for (j = 0; j < ins->modules[i].nsegments; ++ j) {
 536			struct dsp_segment_desc * desc = (ins->modules[i].segments + j);
 537			snd_iprintf(buffer, "   segment %02x offset %08x size %08x\n",
 538				    desc->segment_type,desc->offset, desc->size);
 539		}
 540	}
 541	mutex_unlock(&chip->spos_mutex);
 542}
 543
 544static void cs46xx_dsp_proc_task_tree_read (struct snd_info_entry *entry,
 545					    struct snd_info_buffer *buffer)
 546{
 547	struct snd_cs46xx *chip = entry->private_data;
 548	struct dsp_spos_instance * ins = chip->dsp_spos_instance;
 549	int i, j, col;
 550	void __iomem *dst = chip->region.idx[1].remap_addr + DSP_PARAMETER_BYTE_OFFSET;
 551
 552	mutex_lock(&chip->spos_mutex);
 553	snd_iprintf(buffer, "TASK TREES:\n");
 554	for ( i = 0; i < ins->ntask; ++i) {
 555		snd_iprintf(buffer,"\n%04x %s:\n",ins->tasks[i].address,ins->tasks[i].task_name);
 556
 557		for (col = 0,j = 0;j < ins->tasks[i].size; j++,col++) {
 558			u32 val;
 559			if (col == 4) {
 560				snd_iprintf(buffer,"\n");
 561				col = 0;
 562			}
 563			val = readl(dst + (ins->tasks[i].address + j) * sizeof(u32));
 564			snd_iprintf(buffer,"%08x ",val);
 565		}
 566	}
 567
 568	snd_iprintf(buffer,"\n");  
 569	mutex_unlock(&chip->spos_mutex);
 570}
 571
 572static void cs46xx_dsp_proc_scb_read (struct snd_info_entry *entry,
 573				      struct snd_info_buffer *buffer)
 574{
 575	struct snd_cs46xx *chip = entry->private_data;
 576	struct dsp_spos_instance * ins = chip->dsp_spos_instance;
 577	int i;
 578
 579	mutex_lock(&chip->spos_mutex);
 580	snd_iprintf(buffer, "SCB's:\n");
 581	for ( i = 0; i < ins->nscb; ++i) {
 582		if (ins->scbs[i].deleted)
 583			continue;
 584		snd_iprintf(buffer,"\n%04x %s:\n\n",ins->scbs[i].address,ins->scbs[i].scb_name);
 585
 586		if (ins->scbs[i].parent_scb_ptr != NULL) {
 587			snd_iprintf(buffer,"parent [%s:%04x] ", 
 588				    ins->scbs[i].parent_scb_ptr->scb_name,
 589				    ins->scbs[i].parent_scb_ptr->address);
 590		} else snd_iprintf(buffer,"parent [none] ");
 591
 592		snd_iprintf(buffer,"sub_list_ptr [%s:%04x]\nnext_scb_ptr [%s:%04x]  task_entry [%s:%04x]\n",
 593			    ins->scbs[i].sub_list_ptr->scb_name,
 594			    ins->scbs[i].sub_list_ptr->address,
 595			    ins->scbs[i].next_scb_ptr->scb_name,
 596			    ins->scbs[i].next_scb_ptr->address,
 597			    ins->scbs[i].task_entry->symbol_name,
 598			    ins->scbs[i].task_entry->address);
 599	}
 600
 601	snd_iprintf(buffer,"\n");
 602	mutex_unlock(&chip->spos_mutex);
 603}
 604
 605static void cs46xx_dsp_proc_parameter_dump_read (struct snd_info_entry *entry,
 606						 struct snd_info_buffer *buffer)
 607{
 608	struct snd_cs46xx *chip = entry->private_data;
 609	/*struct dsp_spos_instance * ins = chip->dsp_spos_instance; */
 610	unsigned int i, col = 0;
 611	void __iomem *dst = chip->region.idx[1].remap_addr + DSP_PARAMETER_BYTE_OFFSET;
 612	struct dsp_symbol_entry * symbol; 
 613
 614	for (i = 0;i < DSP_PARAMETER_BYTE_SIZE; i += sizeof(u32),col ++) {
 615		if (col == 4) {
 616			snd_iprintf(buffer,"\n");
 617			col = 0;
 618		}
 619
 620		symbol = cs46xx_dsp_lookup_symbol_addr(chip, i / sizeof(u32), SYMBOL_PARAMETER);
 621		if (symbol) {
 622			col = 0;
 623			snd_iprintf (buffer,"\n%s:\n",symbol->symbol_name);
 624		}
 625
 626		if (col == 0) {
 627			snd_iprintf(buffer, "%04X ", i / (unsigned int)sizeof(u32));
 628		}
 629
 630		snd_iprintf(buffer,"%08X ",readl(dst + i));
 631	}
 632}
 633
 634static void cs46xx_dsp_proc_sample_dump_read (struct snd_info_entry *entry,
 635					      struct snd_info_buffer *buffer)
 636{
 637	struct snd_cs46xx *chip = entry->private_data;
 638	int i,col = 0;
 639	void __iomem *dst = chip->region.idx[2].remap_addr;
 640
 641	snd_iprintf(buffer,"PCMREADER:\n");
 642	for (i = PCM_READER_BUF1;i < PCM_READER_BUF1 + 0x30; i += sizeof(u32),col ++) {
 643		if (col == 4) {
 644			snd_iprintf(buffer,"\n");
 645			col = 0;
 646		}
 647
 648		if (col == 0) {
 649			snd_iprintf(buffer, "%04X ",i);
 650		}
 651
 652		snd_iprintf(buffer,"%08X ",readl(dst + i));
 653	}
 654
 655	snd_iprintf(buffer,"\nMIX_SAMPLE_BUF1:\n");
 656
 657	col = 0;
 658	for (i = MIX_SAMPLE_BUF1;i < MIX_SAMPLE_BUF1 + 0x40; i += sizeof(u32),col ++) {
 659		if (col == 4) {
 660			snd_iprintf(buffer,"\n");
 661			col = 0;
 662		}
 663
 664		if (col == 0) {
 665			snd_iprintf(buffer, "%04X ",i);
 666		}
 667
 668		snd_iprintf(buffer,"%08X ",readl(dst + i));
 669	}
 670
 671	snd_iprintf(buffer,"\nSRC_TASK_SCB1:\n");
 672	col = 0;
 673	for (i = 0x2480 ; i < 0x2480 + 0x40 ; i += sizeof(u32),col ++) {
 674		if (col == 4) {
 675			snd_iprintf(buffer,"\n");
 676			col = 0;
 677		}
 678		
 679		if (col == 0) {
 680			snd_iprintf(buffer, "%04X ",i);
 681		}
 682
 683		snd_iprintf(buffer,"%08X ",readl(dst + i));
 684	}
 685
 686
 687	snd_iprintf(buffer,"\nSPDIFO_BUFFER:\n");
 688	col = 0;
 689	for (i = SPDIFO_IP_OUTPUT_BUFFER1;i < SPDIFO_IP_OUTPUT_BUFFER1 + 0x30; i += sizeof(u32),col ++) {
 690		if (col == 4) {
 691			snd_iprintf(buffer,"\n");
 692			col = 0;
 693		}
 694
 695		if (col == 0) {
 696			snd_iprintf(buffer, "%04X ",i);
 697		}
 698
 699		snd_iprintf(buffer,"%08X ",readl(dst + i));
 700	}
 701
 702	snd_iprintf(buffer,"\n...\n");
 703	col = 0;
 704
 705	for (i = SPDIFO_IP_OUTPUT_BUFFER1+0xD0;i < SPDIFO_IP_OUTPUT_BUFFER1 + 0x110; i += sizeof(u32),col ++) {
 706		if (col == 4) {
 707			snd_iprintf(buffer,"\n");
 708			col = 0;
 709		}
 710
 711		if (col == 0) {
 712			snd_iprintf(buffer, "%04X ",i);
 713		}
 714
 715		snd_iprintf(buffer,"%08X ",readl(dst + i));
 716	}
 717
 718
 719	snd_iprintf(buffer,"\nOUTPUT_SNOOP:\n");
 720	col = 0;
 721	for (i = OUTPUT_SNOOP_BUFFER;i < OUTPUT_SNOOP_BUFFER + 0x40; i += sizeof(u32),col ++) {
 722		if (col == 4) {
 723			snd_iprintf(buffer,"\n");
 724			col = 0;
 725		}
 726
 727		if (col == 0) {
 728			snd_iprintf(buffer, "%04X ",i);
 729		}
 730
 731		snd_iprintf(buffer,"%08X ",readl(dst + i));
 732	}
 733
 734	snd_iprintf(buffer,"\nCODEC_INPUT_BUF1: \n");
 735	col = 0;
 736	for (i = CODEC_INPUT_BUF1;i < CODEC_INPUT_BUF1 + 0x40; i += sizeof(u32),col ++) {
 737		if (col == 4) {
 738			snd_iprintf(buffer,"\n");
 739			col = 0;
 740		}
 741
 742		if (col == 0) {
 743			snd_iprintf(buffer, "%04X ",i);
 744		}
 745
 746		snd_iprintf(buffer,"%08X ",readl(dst + i));
 747	}
 748#if 0
 749	snd_iprintf(buffer,"\nWRITE_BACK_BUF1: \n");
 750	col = 0;
 751	for (i = WRITE_BACK_BUF1;i < WRITE_BACK_BUF1 + 0x40; i += sizeof(u32),col ++) {
 752		if (col == 4) {
 753			snd_iprintf(buffer,"\n");
 754			col = 0;
 755		}
 756
 757		if (col == 0) {
 758			snd_iprintf(buffer, "%04X ",i);
 759		}
 760
 761		snd_iprintf(buffer,"%08X ",readl(dst + i));
 762	}
 763#endif
 764
 765	snd_iprintf(buffer,"\nSPDIFI_IP_OUTPUT_BUFFER1: \n");
 766	col = 0;
 767	for (i = SPDIFI_IP_OUTPUT_BUFFER1;i < SPDIFI_IP_OUTPUT_BUFFER1 + 0x80; i += sizeof(u32),col ++) {
 768		if (col == 4) {
 769			snd_iprintf(buffer,"\n");
 770			col = 0;
 771		}
 772
 773		if (col == 0) {
 774			snd_iprintf(buffer, "%04X ",i);
 775		}
 776		
 777		snd_iprintf(buffer,"%08X ",readl(dst + i));
 778	}
 779	snd_iprintf(buffer,"\n");
 780}
 781
 782int cs46xx_dsp_proc_init (struct snd_card *card, struct snd_cs46xx *chip)
 783{
 784	struct snd_info_entry *entry;
 785	struct dsp_spos_instance * ins = chip->dsp_spos_instance;
 786	int i;
 787
 788	ins->snd_card = card;
 789
 790	entry = snd_info_create_card_entry(card, "dsp", card->proc_root);
 791	if (entry)
 792		entry->mode = S_IFDIR | 0555;
 
 
 
 
 
 
 
 793	ins->proc_dsp_dir = entry;
 794
 795	if (!ins->proc_dsp_dir)
 796		return -ENOMEM;
 797
 798	entry = snd_info_create_card_entry(card, "spos_symbols",
 799					   ins->proc_dsp_dir);
 800	if (entry)
 801		snd_info_set_text_ops(entry, chip,
 802				      cs46xx_dsp_proc_symbol_table_read);
 
 
 
 
 
 
 803    
 804	entry = snd_info_create_card_entry(card, "spos_modules",
 805					   ins->proc_dsp_dir);
 806	if (entry)
 807		snd_info_set_text_ops(entry, chip,
 808				      cs46xx_dsp_proc_modules_read);
 809
 810	entry = snd_info_create_card_entry(card, "parameter",
 811					   ins->proc_dsp_dir);
 812	if (entry)
 813		snd_info_set_text_ops(entry, chip,
 814				      cs46xx_dsp_proc_parameter_dump_read);
 815
 816	entry = snd_info_create_card_entry(card, "sample",
 817					   ins->proc_dsp_dir);
 818	if (entry)
 819		snd_info_set_text_ops(entry, chip,
 820				      cs46xx_dsp_proc_sample_dump_read);
 821
 822	entry = snd_info_create_card_entry(card, "task_tree",
 823					   ins->proc_dsp_dir);
 824	if (entry)
 825		snd_info_set_text_ops(entry, chip,
 826				      cs46xx_dsp_proc_task_tree_read);
 827
 828	entry = snd_info_create_card_entry(card, "scb_info",
 829					   ins->proc_dsp_dir);
 830	if (entry)
 831		snd_info_set_text_ops(entry, chip,
 832				      cs46xx_dsp_proc_scb_read);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 833
 834	mutex_lock(&chip->spos_mutex);
 835	/* register/update SCB's entries on proc */
 836	for (i = 0; i < ins->nscb; ++i) {
 837		if (ins->scbs[i].deleted) continue;
 838
 839		cs46xx_dsp_proc_register_scb_desc (chip, (ins->scbs + i));
 840	}
 841	mutex_unlock(&chip->spos_mutex);
 842
 843	return 0;
 844}
 845
 846int cs46xx_dsp_proc_done (struct snd_cs46xx *chip)
 847{
 848	struct dsp_spos_instance * ins = chip->dsp_spos_instance;
 849	int i;
 850
 851	if (!ins)
 852		return 0;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 853
 854	mutex_lock(&chip->spos_mutex);
 855	for (i = 0; i < ins->nscb; ++i) {
 856		if (ins->scbs[i].deleted) continue;
 857		cs46xx_dsp_proc_free_scb_desc ( (ins->scbs + i) );
 858	}
 859	mutex_unlock(&chip->spos_mutex);
 860
 861	snd_info_free_entry(ins->proc_dsp_dir);
 862	ins->proc_dsp_dir = NULL;
 863
 864	return 0;
 865}
 866#endif /* CONFIG_SND_PROC_FS */
 867
 868static void _dsp_create_task_tree (struct snd_cs46xx *chip, u32 * task_data,
 869				   u32  dest, int size)
 870{
 871	void __iomem *spdst = chip->region.idx[1].remap_addr + 
 872		DSP_PARAMETER_BYTE_OFFSET + dest * sizeof(u32);
 873	int i;
 874
 875	for (i = 0; i < size; ++i) {
 876		dev_dbg(chip->card->dev, "addr %p, val %08x\n",
 877			spdst, task_data[i]);
 878		writel(task_data[i],spdst);
 879		spdst += sizeof(u32);
 880	}
 881}
 882
 883static void _dsp_create_scb (struct snd_cs46xx *chip, u32 * scb_data, u32 dest)
 884{
 885	void __iomem *spdst = chip->region.idx[1].remap_addr + 
 886		DSP_PARAMETER_BYTE_OFFSET + dest * sizeof(u32);
 887	int i;
 888
 889	for (i = 0; i < 0x10; ++i) {
 890		dev_dbg(chip->card->dev, "addr %p, val %08x\n",
 891			spdst, scb_data[i]);
 892		writel(scb_data[i],spdst);
 893		spdst += sizeof(u32);
 894	}
 895}
 896
 897static int find_free_scb_index (struct dsp_spos_instance * ins)
 898{
 899	int index = ins->nscb, i;
 900
 901	for (i = ins->scb_highest_frag_index; i < ins->nscb; ++i) {
 902		if (ins->scbs[i].deleted) {
 903			index = i;
 904			break;
 905		}
 906	}
 907
 908	return index;
 909}
 910
 911static struct dsp_scb_descriptor * _map_scb (struct snd_cs46xx *chip, char * name, u32 dest)
 912{
 913	struct dsp_spos_instance * ins = chip->dsp_spos_instance;
 914	struct dsp_scb_descriptor * desc = NULL;
 915	int index;
 916
 917	if (ins->nscb == DSP_MAX_SCB_DESC - 1) {
 918		dev_err(chip->card->dev,
 919			"dsp_spos: got no place for other SCB\n");
 920		return NULL;
 921	}
 922
 923	index = find_free_scb_index (ins);
 924
 925	memset(&ins->scbs[index], 0, sizeof(ins->scbs[index]));
 926	strcpy(ins->scbs[index].scb_name, name);
 927	ins->scbs[index].address = dest;
 928	ins->scbs[index].index = index;
 929	ins->scbs[index].ref_count = 1;
 930
 931	desc = (ins->scbs + index);
 932	ins->scbs[index].scb_symbol = add_symbol (chip, name, dest, SYMBOL_PARAMETER);
 933
 934	if (index > ins->scb_highest_frag_index)
 935		ins->scb_highest_frag_index = index;
 936
 937	if (index == ins->nscb)
 938		ins->nscb++;
 939
 940	return desc;
 941}
 942
 943static struct dsp_task_descriptor *
 944_map_task_tree (struct snd_cs46xx *chip, char * name, u32 dest, u32 size)
 945{
 946	struct dsp_spos_instance * ins = chip->dsp_spos_instance;
 947	struct dsp_task_descriptor * desc = NULL;
 948
 949	if (ins->ntask == DSP_MAX_TASK_DESC - 1) {
 950		dev_err(chip->card->dev,
 951			"dsp_spos: got no place for other TASK\n");
 952		return NULL;
 953	}
 954
 955	if (name)
 956		strcpy(ins->tasks[ins->ntask].task_name, name);
 957	else
 958		strcpy(ins->tasks[ins->ntask].task_name, "(NULL)");
 959	ins->tasks[ins->ntask].address = dest;
 960	ins->tasks[ins->ntask].size = size;
 961
 962	/* quick find in list */
 963	ins->tasks[ins->ntask].index = ins->ntask;
 964	desc = (ins->tasks + ins->ntask);
 965	ins->ntask++;
 966
 967	if (name)
 968		add_symbol (chip,name,dest,SYMBOL_PARAMETER);
 969	return desc;
 970}
 971
 972#define SCB_BYTES	(0x10 * 4)
 973
 974struct dsp_scb_descriptor *
 975cs46xx_dsp_create_scb (struct snd_cs46xx *chip, char * name, u32 * scb_data, u32 dest)
 976{
 977	struct dsp_scb_descriptor * desc;
 978
 979#ifdef CONFIG_PM_SLEEP
 980	/* copy the data for resume */
 981	scb_data = kmemdup(scb_data, SCB_BYTES, GFP_KERNEL);
 982	if (!scb_data)
 983		return NULL;
 984#endif
 985
 986	desc = _map_scb (chip,name,dest);
 987	if (desc) {
 988		desc->data = scb_data;
 989		_dsp_create_scb(chip,scb_data,dest);
 990	} else {
 991		dev_err(chip->card->dev, "dsp_spos: failed to map SCB\n");
 992#ifdef CONFIG_PM_SLEEP
 993		kfree(scb_data);
 994#endif
 995	}
 996
 997	return desc;
 998}
 999
1000
1001static struct dsp_task_descriptor *
1002cs46xx_dsp_create_task_tree (struct snd_cs46xx *chip, char * name, u32 * task_data,
1003			     u32 dest, int size)
1004{
1005	struct dsp_task_descriptor * desc;
1006
1007	desc = _map_task_tree (chip,name,dest,size);
1008	if (desc) {
1009		desc->data = task_data;
1010		_dsp_create_task_tree(chip,task_data,dest,size);
1011	} else {
1012		dev_err(chip->card->dev, "dsp_spos: failed to map TASK\n");
1013	}
1014
1015	return desc;
1016}
1017
1018int cs46xx_dsp_scb_and_task_init (struct snd_cs46xx *chip)
1019{
1020	struct dsp_spos_instance * ins = chip->dsp_spos_instance;
1021	struct dsp_symbol_entry * fg_task_tree_header_code;
1022	struct dsp_symbol_entry * task_tree_header_code;
1023	struct dsp_symbol_entry * task_tree_thread;
1024	struct dsp_symbol_entry * null_algorithm;
1025	struct dsp_symbol_entry * magic_snoop_task;
1026
1027	struct dsp_scb_descriptor * timing_master_scb;
1028	struct dsp_scb_descriptor * codec_out_scb;
1029	struct dsp_scb_descriptor * codec_in_scb;
1030	struct dsp_scb_descriptor * src_task_scb;
1031	struct dsp_scb_descriptor * master_mix_scb;
1032	struct dsp_scb_descriptor * rear_mix_scb;
1033	struct dsp_scb_descriptor * record_mix_scb;
1034	struct dsp_scb_descriptor * write_back_scb;
1035	struct dsp_scb_descriptor * vari_decimate_scb;
1036	struct dsp_scb_descriptor * rear_codec_out_scb;
1037	struct dsp_scb_descriptor * clfe_codec_out_scb;
1038	struct dsp_scb_descriptor * magic_snoop_scb;
1039	
1040	int fifo_addr, fifo_span, valid_slots;
1041
1042	static const struct dsp_spos_control_block sposcb = {
1043		/* 0 */ HFG_TREE_SCB,HFG_STACK,
1044		/* 1 */ SPOSCB_ADDR,BG_TREE_SCB_ADDR,
1045		/* 2 */ DSP_SPOS_DC,0,
1046		/* 3 */ DSP_SPOS_DC,DSP_SPOS_DC,
1047		/* 4 */ 0,0,
1048		/* 5 */ DSP_SPOS_UU,0,
1049		/* 6 */ FG_TASK_HEADER_ADDR,0,
1050		/* 7 */ 0,0,
1051		/* 8 */ DSP_SPOS_UU,DSP_SPOS_DC,
1052		/* 9 */ 0,
1053		/* A */ 0,HFG_FIRST_EXECUTE_MODE,
1054		/* B */ DSP_SPOS_UU,DSP_SPOS_UU,
1055		/* C */ DSP_SPOS_DC_DC,
1056		/* D */ DSP_SPOS_DC_DC,
1057		/* E */ DSP_SPOS_DC_DC,
1058		/* F */ DSP_SPOS_DC_DC
1059	};
1060
1061	cs46xx_dsp_create_task_tree(chip, "sposCB", (u32 *)&sposcb, SPOSCB_ADDR, 0x10);
1062
1063	null_algorithm  = cs46xx_dsp_lookup_symbol(chip, "NULLALGORITHM", SYMBOL_CODE);
1064	if (null_algorithm == NULL) {
1065		dev_err(chip->card->dev,
1066			"dsp_spos: symbol NULLALGORITHM not found\n");
1067		return -EIO;
1068	}
1069
1070	fg_task_tree_header_code = cs46xx_dsp_lookup_symbol(chip, "FGTASKTREEHEADERCODE", SYMBOL_CODE);  
1071	if (fg_task_tree_header_code == NULL) {
1072		dev_err(chip->card->dev,
1073			"dsp_spos: symbol FGTASKTREEHEADERCODE not found\n");
1074		return -EIO;
1075	}
1076
1077	task_tree_header_code = cs46xx_dsp_lookup_symbol(chip, "TASKTREEHEADERCODE", SYMBOL_CODE);  
1078	if (task_tree_header_code == NULL) {
1079		dev_err(chip->card->dev,
1080			"dsp_spos: symbol TASKTREEHEADERCODE not found\n");
1081		return -EIO;
1082	}
1083  
1084	task_tree_thread = cs46xx_dsp_lookup_symbol(chip, "TASKTREETHREAD", SYMBOL_CODE);
1085	if (task_tree_thread == NULL) {
1086		dev_err(chip->card->dev,
1087			"dsp_spos: symbol TASKTREETHREAD not found\n");
1088		return -EIO;
1089	}
1090
1091	magic_snoop_task = cs46xx_dsp_lookup_symbol(chip, "MAGICSNOOPTASK", SYMBOL_CODE);
1092	if (magic_snoop_task == NULL) {
1093		dev_err(chip->card->dev,
1094			"dsp_spos: symbol MAGICSNOOPTASK not found\n");
1095		return -EIO;
1096	}
1097  
1098	{
1099		/* create the null SCB */
1100		static struct dsp_generic_scb null_scb = {
1101			{ 0, 0, 0, 0 },
1102			{ 0, 0, 0, 0, 0 },
1103			NULL_SCB_ADDR, NULL_SCB_ADDR,
1104			0, 0, 0, 0, 0,
1105			{
1106				0,0,
1107				0,0,
1108			}
1109		};
1110
1111		null_scb.entry_point = null_algorithm->address;
1112		ins->the_null_scb = cs46xx_dsp_create_scb(chip, "nullSCB", (u32 *)&null_scb, NULL_SCB_ADDR);
1113		ins->the_null_scb->task_entry = null_algorithm;
1114		ins->the_null_scb->sub_list_ptr = ins->the_null_scb;
1115		ins->the_null_scb->next_scb_ptr = ins->the_null_scb;
1116		ins->the_null_scb->parent_scb_ptr = NULL;
1117		cs46xx_dsp_proc_register_scb_desc (chip,ins->the_null_scb);
1118	}
1119
1120	{
1121		/* setup foreground task tree */
1122		static struct dsp_task_tree_control_block fg_task_tree_hdr =  {
1123			{ FG_TASK_HEADER_ADDR | (DSP_SPOS_DC << 0x10),
1124			  DSP_SPOS_DC_DC,
1125			  DSP_SPOS_DC_DC,
1126			  0x0000,DSP_SPOS_DC,
1127			  DSP_SPOS_DC, DSP_SPOS_DC,
1128			  DSP_SPOS_DC_DC,
1129			  DSP_SPOS_DC_DC,
1130			  DSP_SPOS_DC_DC,
1131			  DSP_SPOS_DC,DSP_SPOS_DC },
1132    
1133			{
1134				BG_TREE_SCB_ADDR,TIMINGMASTER_SCB_ADDR, 
1135				0,
1136				FG_TASK_HEADER_ADDR + TCBData,                  
1137			},
1138
1139			{    
1140				4,0,
1141				1,0,
1142				2,SPOSCB_ADDR + HFGFlags,
1143				0,0,
1144				FG_TASK_HEADER_ADDR + TCBContextBlk,FG_STACK
1145			},
1146
1147			{
1148				DSP_SPOS_DC,0,
1149				DSP_SPOS_DC,DSP_SPOS_DC,
1150				DSP_SPOS_DC,DSP_SPOS_DC,
1151				DSP_SPOS_DC,DSP_SPOS_DC,
1152				DSP_SPOS_DC,DSP_SPOS_DC,
1153				DSP_SPOS_DCDC,
1154				DSP_SPOS_UU,1,
1155				DSP_SPOS_DCDC,
1156				DSP_SPOS_DCDC,
1157				DSP_SPOS_DCDC,
1158				DSP_SPOS_DCDC,
1159				DSP_SPOS_DCDC,
1160				DSP_SPOS_DCDC,
1161				DSP_SPOS_DCDC,
1162				DSP_SPOS_DCDC,
1163				DSP_SPOS_DCDC,
1164				DSP_SPOS_DCDC,
1165				DSP_SPOS_DCDC,
1166				DSP_SPOS_DCDC,
1167				DSP_SPOS_DCDC,
1168				DSP_SPOS_DCDC,
1169				DSP_SPOS_DCDC,
1170				DSP_SPOS_DCDC,
1171				DSP_SPOS_DCDC,
1172				DSP_SPOS_DCDC,
1173				DSP_SPOS_DCDC,
1174				DSP_SPOS_DCDC,
1175				DSP_SPOS_DCDC,
1176				DSP_SPOS_DCDC,
1177				DSP_SPOS_DCDC,
1178				DSP_SPOS_DCDC,
1179				DSP_SPOS_DCDC,
1180				DSP_SPOS_DCDC,
1181				DSP_SPOS_DCDC,
1182				DSP_SPOS_DCDC 
1183			},                                               
1184			{ 
1185				FG_INTERVAL_TIMER_PERIOD,DSP_SPOS_UU,
1186				0,0
1187			}
1188		};
1189
1190		fg_task_tree_hdr.links.entry_point = fg_task_tree_header_code->address;
1191		fg_task_tree_hdr.context_blk.stack0 = task_tree_thread->address;
1192		cs46xx_dsp_create_task_tree(chip,"FGtaskTreeHdr",(u32 *)&fg_task_tree_hdr,FG_TASK_HEADER_ADDR,0x35);
1193	}
1194
1195
1196	{
1197		/* setup foreground task tree */
1198		static struct dsp_task_tree_control_block bg_task_tree_hdr =  {
1199			{ DSP_SPOS_DC_DC,
1200			  DSP_SPOS_DC_DC,
1201			  DSP_SPOS_DC_DC,
1202			  DSP_SPOS_DC, DSP_SPOS_DC,
1203			  DSP_SPOS_DC, DSP_SPOS_DC,
1204			  DSP_SPOS_DC_DC,
1205			  DSP_SPOS_DC_DC,
1206			  DSP_SPOS_DC_DC,
1207			  DSP_SPOS_DC,DSP_SPOS_DC },
1208    
1209			{
1210				NULL_SCB_ADDR,NULL_SCB_ADDR,  /* Set up the background to do nothing */
1211				0,
1212				BG_TREE_SCB_ADDR + TCBData,
1213			},
1214
1215			{    
1216				9999,0,
1217				0,1,
1218				0,SPOSCB_ADDR + HFGFlags,
1219				0,0,
1220				BG_TREE_SCB_ADDR + TCBContextBlk,BG_STACK
1221			},
1222
1223			{
1224				DSP_SPOS_DC,0,
1225				DSP_SPOS_DC,DSP_SPOS_DC,
1226				DSP_SPOS_DC,DSP_SPOS_DC,
1227				DSP_SPOS_DC,DSP_SPOS_DC,
1228				DSP_SPOS_DC,DSP_SPOS_DC,
1229				DSP_SPOS_DCDC,
1230				DSP_SPOS_UU,1,
1231				DSP_SPOS_DCDC,
1232				DSP_SPOS_DCDC,
1233				DSP_SPOS_DCDC,
1234				DSP_SPOS_DCDC,
1235				DSP_SPOS_DCDC,
1236				DSP_SPOS_DCDC,
1237				DSP_SPOS_DCDC,
1238				DSP_SPOS_DCDC,
1239				DSP_SPOS_DCDC,
1240				DSP_SPOS_DCDC,
1241				DSP_SPOS_DCDC,
1242				DSP_SPOS_DCDC,
1243				DSP_SPOS_DCDC,
1244				DSP_SPOS_DCDC,
1245				DSP_SPOS_DCDC,
1246				DSP_SPOS_DCDC,
1247				DSP_SPOS_DCDC,
1248				DSP_SPOS_DCDC,
1249				DSP_SPOS_DCDC,
1250				DSP_SPOS_DCDC,
1251				DSP_SPOS_DCDC,
1252				DSP_SPOS_DCDC,
1253				DSP_SPOS_DCDC,
1254				DSP_SPOS_DCDC,
1255				DSP_SPOS_DCDC,
1256				DSP_SPOS_DCDC,
1257				DSP_SPOS_DCDC,
1258				DSP_SPOS_DCDC 
1259			},                                               
1260			{ 
1261				BG_INTERVAL_TIMER_PERIOD,DSP_SPOS_UU,
1262				0,0
1263			}
1264		};
1265
1266		bg_task_tree_hdr.links.entry_point = task_tree_header_code->address;
1267		bg_task_tree_hdr.context_blk.stack0 = task_tree_thread->address;
1268		cs46xx_dsp_create_task_tree(chip,"BGtaskTreeHdr",(u32 *)&bg_task_tree_hdr,BG_TREE_SCB_ADDR,0x35);
1269	}
1270
1271	/* create timing master SCB */
1272	timing_master_scb = cs46xx_dsp_create_timing_master_scb(chip);
1273
1274	/* create the CODEC output task */
1275	codec_out_scb = cs46xx_dsp_create_codec_out_scb(chip,"CodecOutSCB_I",0x0010,0x0000,
1276							MASTERMIX_SCB_ADDR,
1277							CODECOUT_SCB_ADDR,timing_master_scb,
1278							SCB_ON_PARENT_SUBLIST_SCB);
1279
1280	if (!codec_out_scb) goto _fail_end;
1281	/* create the master mix SCB */
1282	master_mix_scb = cs46xx_dsp_create_mix_only_scb(chip,"MasterMixSCB",
1283							MIX_SAMPLE_BUF1,MASTERMIX_SCB_ADDR,
1284							codec_out_scb,
1285							SCB_ON_PARENT_SUBLIST_SCB);
1286	ins->master_mix_scb = master_mix_scb;
1287
1288	if (!master_mix_scb) goto _fail_end;
1289
1290	/* create codec in */
1291	codec_in_scb = cs46xx_dsp_create_codec_in_scb(chip,"CodecInSCB",0x0010,0x00A0,
1292						      CODEC_INPUT_BUF1,
1293						      CODECIN_SCB_ADDR,codec_out_scb,
1294						      SCB_ON_PARENT_NEXT_SCB);
1295	if (!codec_in_scb) goto _fail_end;
1296	ins->codec_in_scb = codec_in_scb;
1297
1298	/* create write back scb */
1299	write_back_scb = cs46xx_dsp_create_mix_to_ostream_scb(chip,"WriteBackSCB",
1300							      WRITE_BACK_BUF1,WRITE_BACK_SPB,
1301							      WRITEBACK_SCB_ADDR,
1302							      timing_master_scb,
1303							      SCB_ON_PARENT_NEXT_SCB);
1304	if (!write_back_scb) goto _fail_end;
1305
1306	{
1307		static struct dsp_mix2_ostream_spb mix2_ostream_spb = {
1308			0x00020000,
1309			0x0000ffff
1310		};
1311    
1312		if (!cs46xx_dsp_create_task_tree(chip, NULL,
1313						 (u32 *)&mix2_ostream_spb,
1314						 WRITE_BACK_SPB, 2))
1315			goto _fail_end;
1316	}
1317
1318	/* input sample converter */
1319	vari_decimate_scb = cs46xx_dsp_create_vari_decimate_scb(chip,"VariDecimateSCB",
1320								VARI_DECIMATE_BUF0,
1321								VARI_DECIMATE_BUF1,
1322								VARIDECIMATE_SCB_ADDR,
1323								write_back_scb,
1324								SCB_ON_PARENT_SUBLIST_SCB);
1325	if (!vari_decimate_scb) goto _fail_end;
1326
1327	/* create the record mixer SCB */
1328	record_mix_scb = cs46xx_dsp_create_mix_only_scb(chip,"RecordMixerSCB",
1329							MIX_SAMPLE_BUF2,
1330							RECORD_MIXER_SCB_ADDR,
1331							vari_decimate_scb,
1332							SCB_ON_PARENT_SUBLIST_SCB);
1333	ins->record_mixer_scb = record_mix_scb;
1334
1335	if (!record_mix_scb) goto _fail_end;
1336
1337	valid_slots = snd_cs46xx_peekBA0(chip, BA0_ACOSV);
1338
1339	if (snd_BUG_ON(chip->nr_ac97_codecs != 1 && chip->nr_ac97_codecs != 2))
1340		goto _fail_end;
1341
1342	if (chip->nr_ac97_codecs == 1) {
1343		/* output on slot 5 and 11 
1344		   on primary CODEC */
1345		fifo_addr = 0x20;
1346		fifo_span = 0x60;
1347
1348		/* enable slot 5 and 11 */
1349		valid_slots |= ACOSV_SLV5 | ACOSV_SLV11;
1350	} else {
1351		/* output on slot 7 and 8 
1352		   on secondary CODEC */
1353		fifo_addr = 0x40;
1354		fifo_span = 0x10;
1355
1356		/* enable slot 7 and 8 */
1357		valid_slots |= ACOSV_SLV7 | ACOSV_SLV8;
1358	}
1359	/* create CODEC tasklet for rear speakers output*/
1360	rear_codec_out_scb = cs46xx_dsp_create_codec_out_scb(chip,"CodecOutSCB_Rear",fifo_span,fifo_addr,
1361							     REAR_MIXER_SCB_ADDR,
1362							     REAR_CODECOUT_SCB_ADDR,codec_in_scb,
1363							     SCB_ON_PARENT_NEXT_SCB);
1364	if (!rear_codec_out_scb) goto _fail_end;
1365	
1366	
1367	/* create the rear PCM channel  mixer SCB */
1368	rear_mix_scb = cs46xx_dsp_create_mix_only_scb(chip,"RearMixerSCB",
1369						      MIX_SAMPLE_BUF3,
1370						      REAR_MIXER_SCB_ADDR,
1371						      rear_codec_out_scb,
1372						      SCB_ON_PARENT_SUBLIST_SCB);
1373	ins->rear_mix_scb = rear_mix_scb;
1374	if (!rear_mix_scb) goto _fail_end;
1375	
1376	if (chip->nr_ac97_codecs == 2) {
1377		/* create CODEC tasklet for rear Center/LFE output 
1378		   slot 6 and 9 on secondary CODEC */
1379		clfe_codec_out_scb = cs46xx_dsp_create_codec_out_scb(chip,"CodecOutSCB_CLFE",0x0030,0x0030,
1380								     CLFE_MIXER_SCB_ADDR,
1381								     CLFE_CODEC_SCB_ADDR,
1382								     rear_codec_out_scb,
1383								     SCB_ON_PARENT_NEXT_SCB);
1384		if (!clfe_codec_out_scb) goto _fail_end;
1385		
1386		
1387		/* create the rear PCM channel  mixer SCB */
1388		ins->center_lfe_mix_scb = cs46xx_dsp_create_mix_only_scb(chip,"CLFEMixerSCB",
1389									 MIX_SAMPLE_BUF4,
1390									 CLFE_MIXER_SCB_ADDR,
1391									 clfe_codec_out_scb,
1392									 SCB_ON_PARENT_SUBLIST_SCB);
1393		if (!ins->center_lfe_mix_scb) goto _fail_end;
1394
1395		/* enable slot 6 and 9 */
1396		valid_slots |= ACOSV_SLV6 | ACOSV_SLV9;
1397	} else {
1398		clfe_codec_out_scb = rear_codec_out_scb;
1399		ins->center_lfe_mix_scb = rear_mix_scb;
1400	}
1401
1402	/* enable slots depending on CODEC configuration */
1403	snd_cs46xx_pokeBA0(chip, BA0_ACOSV, valid_slots);
1404
1405	/* the magic snooper */
1406	magic_snoop_scb = cs46xx_dsp_create_magic_snoop_scb (chip,"MagicSnoopSCB_I",OUTPUTSNOOP_SCB_ADDR,
1407							     OUTPUT_SNOOP_BUFFER,
1408							     codec_out_scb,
1409							     clfe_codec_out_scb,
1410							     SCB_ON_PARENT_NEXT_SCB);
1411
1412    
1413	if (!magic_snoop_scb) goto _fail_end;
1414	ins->ref_snoop_scb = magic_snoop_scb;
1415
1416	/* SP IO access */
1417	if (!cs46xx_dsp_create_spio_write_scb(chip,"SPIOWriteSCB",SPIOWRITE_SCB_ADDR,
1418					      magic_snoop_scb,
1419					      SCB_ON_PARENT_NEXT_SCB))
1420		goto _fail_end;
1421
1422	/* SPDIF input sampel rate converter */
1423	src_task_scb = cs46xx_dsp_create_src_task_scb(chip,"SrcTaskSCB_SPDIFI",
1424						      ins->spdif_in_sample_rate,
1425						      SRC_OUTPUT_BUF1,
1426						      SRC_DELAY_BUF1,SRCTASK_SCB_ADDR,
1427						      master_mix_scb,
1428						      SCB_ON_PARENT_SUBLIST_SCB,1);
1429
1430	if (!src_task_scb) goto _fail_end;
1431	cs46xx_src_unlink(chip,src_task_scb);
1432
1433	/* NOTE: when we now how to detect the SPDIF input
1434	   sample rate we will use this SRC to adjust it */
1435	ins->spdif_in_src = src_task_scb;
1436
1437	cs46xx_dsp_async_init(chip,timing_master_scb);
1438	return 0;
1439
1440 _fail_end:
1441	dev_err(chip->card->dev, "dsp_spos: failed to setup SCB's in DSP\n");
1442	return -EINVAL;
1443}
1444
1445static int cs46xx_dsp_async_init (struct snd_cs46xx *chip,
1446				  struct dsp_scb_descriptor * fg_entry)
1447{
1448	struct dsp_spos_instance * ins = chip->dsp_spos_instance;
1449	struct dsp_symbol_entry * s16_async_codec_input_task;
1450	struct dsp_symbol_entry * spdifo_task;
1451	struct dsp_symbol_entry * spdifi_task;
1452	struct dsp_scb_descriptor * spdifi_scb_desc, * spdifo_scb_desc, * async_codec_scb_desc;
1453
1454	s16_async_codec_input_task = cs46xx_dsp_lookup_symbol(chip, "S16_ASYNCCODECINPUTTASK", SYMBOL_CODE);
1455	if (s16_async_codec_input_task == NULL) {
1456		dev_err(chip->card->dev,
1457			"dsp_spos: symbol S16_ASYNCCODECINPUTTASK not found\n");
1458		return -EIO;
1459	}
1460	spdifo_task = cs46xx_dsp_lookup_symbol(chip, "SPDIFOTASK", SYMBOL_CODE);
1461	if (spdifo_task == NULL) {
1462		dev_err(chip->card->dev,
1463			"dsp_spos: symbol SPDIFOTASK not found\n");
1464		return -EIO;
1465	}
1466
1467	spdifi_task = cs46xx_dsp_lookup_symbol(chip, "SPDIFITASK", SYMBOL_CODE);
1468	if (spdifi_task == NULL) {
1469		dev_err(chip->card->dev,
1470			"dsp_spos: symbol SPDIFITASK not found\n");
1471		return -EIO;
1472	}
1473
1474	{
1475		/* 0xBC0 */
1476		struct dsp_spdifoscb spdifo_scb = {
1477			/* 0 */ DSP_SPOS_UUUU,
1478			{
1479				/* 1 */ 0xb0, 
1480				/* 2 */ 0, 
1481				/* 3 */ 0, 
1482				/* 4 */ 0, 
1483			},
1484			/* NOTE: the SPDIF output task read samples in mono
1485			   format, the AsynchFGTxSCB task writes to buffer
1486			   in stereo format
1487			*/
1488			/* 5 */ RSCONFIG_SAMPLE_16MONO + RSCONFIG_MODULO_256,
1489			/* 6 */ ( SPDIFO_IP_OUTPUT_BUFFER1 << 0x10 )  |  0xFFFC,
1490			/* 7 */ 0,0, 
1491			/* 8 */ 0, 
1492			/* 9 */ FG_TASK_HEADER_ADDR, NULL_SCB_ADDR, 
1493			/* A */ spdifo_task->address,
1494			SPDIFO_SCB_INST + SPDIFOFIFOPointer,
1495			{
1496				/* B */ 0x0040, /*DSP_SPOS_UUUU,*/
1497				/* C */ 0x20ff, /*DSP_SPOS_UUUU,*/
1498			},
1499			/* D */ 0x804c,0,							  /* SPDIFOFIFOPointer:SPDIFOStatRegAddr; */
1500			/* E */ 0x0108,0x0001,					  /* SPDIFOStMoFormat:SPDIFOFIFOBaseAddr; */
1501			/* F */ DSP_SPOS_UUUU	  			          /* SPDIFOFree; */
1502		};
1503
1504		/* 0xBB0 */
1505		struct dsp_spdifiscb spdifi_scb = {
1506			/* 0 */ DSP_SPOS_UULO,DSP_SPOS_UUHI,
1507			/* 1 */ 0,
1508			/* 2 */ 0,
1509			/* 3 */ 1,4000,        /* SPDIFICountLimit SPDIFICount */ 
1510			/* 4 */ DSP_SPOS_UUUU, /* SPDIFIStatusData */
1511			/* 5 */ 0,DSP_SPOS_UUHI, /* StatusData, Free4 */
1512			/* 6 */ DSP_SPOS_UUUU,  /* Free3 */
1513			/* 7 */ DSP_SPOS_UU,DSP_SPOS_DC,  /* Free2 BitCount*/
1514			/* 8 */ DSP_SPOS_UUUU,	/* TempStatus */
1515			/* 9 */ SPDIFO_SCB_INST, NULL_SCB_ADDR,
1516			/* A */ spdifi_task->address,
1517			SPDIFI_SCB_INST + SPDIFIFIFOPointer,
1518			/* NOTE: The SPDIF input task write the sample in mono
1519			   format from the HW FIFO, the AsynchFGRxSCB task  reads 
1520			   them in stereo 
1521			*/
1522			/* B */ RSCONFIG_SAMPLE_16MONO + RSCONFIG_MODULO_128,
1523			/* C */ (SPDIFI_IP_OUTPUT_BUFFER1 << 0x10) | 0xFFFC,
1524			/* D */ 0x8048,0,
1525			/* E */ 0x01f0,0x0001,
1526			/* F */ DSP_SPOS_UUUU /* SPDIN_STATUS monitor */
1527		};
1528
1529		/* 0xBA0 */
1530		struct dsp_async_codec_input_scb async_codec_input_scb = {
1531			/* 0 */ DSP_SPOS_UUUU,
1532			/* 1 */ 0,
1533			/* 2 */ 0,
1534			/* 3 */ 1,4000,
1535			/* 4 */ 0x0118,0x0001,
1536			/* 5 */ RSCONFIG_SAMPLE_16MONO + RSCONFIG_MODULO_64,
1537			/* 6 */ (ASYNC_IP_OUTPUT_BUFFER1 << 0x10) | 0xFFFC,
1538			/* 7 */ DSP_SPOS_UU,0x3,
1539			/* 8 */ DSP_SPOS_UUUU,
1540			/* 9 */ SPDIFI_SCB_INST,NULL_SCB_ADDR,
1541			/* A */ s16_async_codec_input_task->address,
1542			HFG_TREE_SCB + AsyncCIOFIFOPointer,
1543              
1544			/* B */ RSCONFIG_SAMPLE_16STEREO + RSCONFIG_MODULO_64,
1545			/* C */ (ASYNC_IP_OUTPUT_BUFFER1 << 0x10),  /*(ASYNC_IP_OUTPUT_BUFFER1 << 0x10) | 0xFFFC,*/
1546      
1547#ifdef UseASER1Input
1548			/* short AsyncCIFIFOPointer:AsyncCIStatRegAddr;	       
1549			   Init. 0000:8042: for ASER1
1550			   0000:8044: for ASER2 */
1551			/* D */ 0x8042,0,
1552      
1553			/* short AsyncCIStMoFormat:AsyncCIFIFOBaseAddr;
1554			   Init 1 stero:8050 ASER1
1555			   Init 0  mono:8070 ASER2
1556			   Init 1 Stereo : 0100 ASER1 (Set by script) */
1557			/* E */ 0x0100,0x0001,
1558      
1559#endif
1560      
1561#ifdef UseASER2Input
1562			/* short AsyncCIFIFOPointer:AsyncCIStatRegAddr;
1563			   Init. 0000:8042: for ASER1
1564			   0000:8044: for ASER2 */
1565			/* D */ 0x8044,0,
1566      
1567			/* short AsyncCIStMoFormat:AsyncCIFIFOBaseAddr;
1568			   Init 1 stero:8050 ASER1
1569			   Init 0  mono:8070 ASER2
1570			   Init 1 Stereo : 0100 ASER1 (Set by script) */
1571			/* E */ 0x0110,0x0001,
1572      
1573#endif
1574      
1575			/* short AsyncCIOutputBufModulo:AsyncCIFree;
1576			   AsyncCIOutputBufModulo: The modulo size for   
1577			   the output buffer of this task */
1578			/* F */ 0, /* DSP_SPOS_UUUU */
1579		};
1580
1581		spdifo_scb_desc = cs46xx_dsp_create_scb(chip,"SPDIFOSCB",(u32 *)&spdifo_scb,SPDIFO_SCB_INST);
1582
1583		if (snd_BUG_ON(!spdifo_scb_desc))
1584			return -EIO;
1585		spdifi_scb_desc = cs46xx_dsp_create_scb(chip,"SPDIFISCB",(u32 *)&spdifi_scb,SPDIFI_SCB_INST);
1586		if (snd_BUG_ON(!spdifi_scb_desc))
1587			return -EIO;
1588		async_codec_scb_desc = cs46xx_dsp_create_scb(chip,"AsynCodecInputSCB",(u32 *)&async_codec_input_scb, HFG_TREE_SCB);
1589		if (snd_BUG_ON(!async_codec_scb_desc))
1590			return -EIO;
1591
1592		async_codec_scb_desc->parent_scb_ptr = NULL;
1593		async_codec_scb_desc->next_scb_ptr = spdifi_scb_desc;
1594		async_codec_scb_desc->sub_list_ptr = ins->the_null_scb;
1595		async_codec_scb_desc->task_entry = s16_async_codec_input_task;
1596
1597		spdifi_scb_desc->parent_scb_ptr = async_codec_scb_desc;
1598		spdifi_scb_desc->next_scb_ptr = spdifo_scb_desc;
1599		spdifi_scb_desc->sub_list_ptr = ins->the_null_scb;
1600		spdifi_scb_desc->task_entry = spdifi_task;
1601
1602		spdifo_scb_desc->parent_scb_ptr = spdifi_scb_desc;
1603		spdifo_scb_desc->next_scb_ptr = fg_entry;
1604		spdifo_scb_desc->sub_list_ptr = ins->the_null_scb;
1605		spdifo_scb_desc->task_entry = spdifo_task;
1606
1607		/* this one is faked, as the parnet of SPDIFO task
1608		   is the FG task tree */
1609		fg_entry->parent_scb_ptr = spdifo_scb_desc;
1610
1611		/* for proc fs */
1612		cs46xx_dsp_proc_register_scb_desc (chip,spdifo_scb_desc);
1613		cs46xx_dsp_proc_register_scb_desc (chip,spdifi_scb_desc);
1614		cs46xx_dsp_proc_register_scb_desc (chip,async_codec_scb_desc);
1615
1616		/* Async MASTER ENABLE, affects both SPDIF input and output */
1617		snd_cs46xx_pokeBA0(chip, BA0_ASER_MASTER, 0x1 );
1618	}
1619
1620	return 0;
1621}
1622
1623static void cs46xx_dsp_disable_spdif_hw (struct snd_cs46xx *chip)
1624{
1625	struct dsp_spos_instance * ins = chip->dsp_spos_instance;
1626
1627	/* set SPDIF output FIFO slot */
1628	snd_cs46xx_pokeBA0(chip, BA0_ASER_FADDR, 0);
1629
1630	/* SPDIF output MASTER ENABLE */
1631	cs46xx_poke_via_dsp (chip,SP_SPDOUT_CONTROL, 0);
1632
1633	/* right and left validate bit */
1634	/*cs46xx_poke_via_dsp (chip,SP_SPDOUT_CSUV, ins->spdif_csuv_default);*/
1635	cs46xx_poke_via_dsp (chip,SP_SPDOUT_CSUV, 0x0);
1636
1637	/* clear fifo pointer */
1638	cs46xx_poke_via_dsp (chip,SP_SPDIN_FIFOPTR, 0x0);
1639
1640	/* monitor state */
1641	ins->spdif_status_out &= ~DSP_SPDIF_STATUS_HW_ENABLED;
1642}
1643
1644int cs46xx_dsp_enable_spdif_hw (struct snd_cs46xx *chip)
1645{
1646	struct dsp_spos_instance * ins = chip->dsp_spos_instance;
1647
1648	/* if hw-ctrl already enabled, turn off to reset logic ... */
1649	cs46xx_dsp_disable_spdif_hw (chip);
1650	udelay(50);
1651
1652	/* set SPDIF output FIFO slot */
1653	snd_cs46xx_pokeBA0(chip, BA0_ASER_FADDR, ( 0x8000 | ((SP_SPDOUT_FIFO >> 4) << 4) ));
1654
1655	/* SPDIF output MASTER ENABLE */
1656	cs46xx_poke_via_dsp (chip,SP_SPDOUT_CONTROL, 0x80000000);
1657
1658	/* right and left validate bit */
1659	cs46xx_poke_via_dsp (chip,SP_SPDOUT_CSUV, ins->spdif_csuv_default);
1660
1661	/* monitor state */
1662	ins->spdif_status_out |= DSP_SPDIF_STATUS_HW_ENABLED;
1663
1664	return 0;
1665}
1666
1667int cs46xx_dsp_enable_spdif_in (struct snd_cs46xx *chip)
1668{
1669	struct dsp_spos_instance * ins = chip->dsp_spos_instance;
1670
1671	/* turn on amplifier */
1672	chip->active_ctrl(chip, 1);
1673	chip->amplifier_ctrl(chip, 1);
1674
1675	if (snd_BUG_ON(ins->asynch_rx_scb))
1676		return -EINVAL;
1677	if (snd_BUG_ON(!ins->spdif_in_src))
1678		return -EINVAL;
1679
1680	mutex_lock(&chip->spos_mutex);
1681
1682	if ( ! (ins->spdif_status_out & DSP_SPDIF_STATUS_INPUT_CTRL_ENABLED) ) {
1683		/* time countdown enable */
1684		cs46xx_poke_via_dsp (chip,SP_ASER_COUNTDOWN, 0x80000005);
1685		/* NOTE: 80000005 value is just magic. With all values
1686		   that I've tested this one seem to give the best result.
1687		   Got no explication why. (Benny) */
1688
1689		/* SPDIF input MASTER ENABLE */
1690		cs46xx_poke_via_dsp (chip,SP_SPDIN_CONTROL, 0x800003ff);
1691
1692		ins->spdif_status_out |= DSP_SPDIF_STATUS_INPUT_CTRL_ENABLED;
1693	}
1694
1695	/* create and start the asynchronous receiver SCB */
1696	ins->asynch_rx_scb = cs46xx_dsp_create_asynch_fg_rx_scb(chip,"AsynchFGRxSCB",
1697								ASYNCRX_SCB_ADDR,
1698								SPDIFI_SCB_INST,
1699								SPDIFI_IP_OUTPUT_BUFFER1,
1700								ins->spdif_in_src,
1701								SCB_ON_PARENT_SUBLIST_SCB);
1702
1703	spin_lock_irq(&chip->reg_lock);
1704
1705	/* reset SPDIF input sample buffer pointer */
1706	/*snd_cs46xx_poke (chip, (SPDIFI_SCB_INST + 0x0c) << 2,
1707	  (SPDIFI_IP_OUTPUT_BUFFER1 << 0x10) | 0xFFFC);*/
1708
1709	/* reset FIFO ptr */
1710	/*cs46xx_poke_via_dsp (chip,SP_SPDIN_FIFOPTR, 0x0);*/
1711	cs46xx_src_link(chip,ins->spdif_in_src);
1712
1713	/* unmute SRC volume */
1714	cs46xx_dsp_scb_set_volume (chip,ins->spdif_in_src,0x7fff,0x7fff);
1715
1716	spin_unlock_irq(&chip->reg_lock);
1717
1718	/* set SPDIF input sample rate and unmute
1719	   NOTE: only 48khz support for SPDIF input this time */
1720	/* cs46xx_dsp_set_src_sample_rate(chip,ins->spdif_in_src,48000); */
1721
1722	/* monitor state */
1723	ins->spdif_status_in = 1;
1724	mutex_unlock(&chip->spos_mutex);
1725
1726	return 0;
1727}
1728
1729int cs46xx_dsp_disable_spdif_in (struct snd_cs46xx *chip)
1730{
1731	struct dsp_spos_instance * ins = chip->dsp_spos_instance;
1732
1733	if (snd_BUG_ON(!ins->asynch_rx_scb))
1734		return -EINVAL;
1735	if (snd_BUG_ON(!ins->spdif_in_src))
1736		return -EINVAL;
1737
1738	mutex_lock(&chip->spos_mutex);
1739
1740	/* Remove the asynchronous receiver SCB */
1741	cs46xx_dsp_remove_scb (chip,ins->asynch_rx_scb);
1742	ins->asynch_rx_scb = NULL;
1743
1744	cs46xx_src_unlink(chip,ins->spdif_in_src);
1745
1746	/* monitor state */
1747	ins->spdif_status_in = 0;
1748	mutex_unlock(&chip->spos_mutex);
1749
1750	/* restore amplifier */
1751	chip->active_ctrl(chip, -1);
1752	chip->amplifier_ctrl(chip, -1);
1753
1754	return 0;
1755}
1756
1757int cs46xx_dsp_enable_pcm_capture (struct snd_cs46xx *chip)
1758{
1759	struct dsp_spos_instance * ins = chip->dsp_spos_instance;
1760
1761	if (snd_BUG_ON(ins->pcm_input))
1762		return -EINVAL;
1763	if (snd_BUG_ON(!ins->ref_snoop_scb))
1764		return -EINVAL;
1765
1766	mutex_lock(&chip->spos_mutex);
1767	ins->pcm_input = cs46xx_add_record_source(chip,ins->ref_snoop_scb,PCMSERIALIN_PCM_SCB_ADDR,
1768                                                  "PCMSerialInput_Wave");
1769	mutex_unlock(&chip->spos_mutex);
1770
1771	return 0;
1772}
1773
1774int cs46xx_dsp_disable_pcm_capture (struct snd_cs46xx *chip)
1775{
1776	struct dsp_spos_instance * ins = chip->dsp_spos_instance;
1777
1778	if (snd_BUG_ON(!ins->pcm_input))
1779		return -EINVAL;
1780
1781	mutex_lock(&chip->spos_mutex);
1782	cs46xx_dsp_remove_scb (chip,ins->pcm_input);
1783	ins->pcm_input = NULL;
1784	mutex_unlock(&chip->spos_mutex);
1785
1786	return 0;
1787}
1788
1789int cs46xx_dsp_enable_adc_capture (struct snd_cs46xx *chip)
1790{
1791	struct dsp_spos_instance * ins = chip->dsp_spos_instance;
1792
1793	if (snd_BUG_ON(ins->adc_input))
1794		return -EINVAL;
1795	if (snd_BUG_ON(!ins->codec_in_scb))
1796		return -EINVAL;
1797
1798	mutex_lock(&chip->spos_mutex);
1799	ins->adc_input = cs46xx_add_record_source(chip,ins->codec_in_scb,PCMSERIALIN_SCB_ADDR,
1800						  "PCMSerialInput_ADC");
1801	mutex_unlock(&chip->spos_mutex);
1802
1803	return 0;
1804}
1805
1806int cs46xx_dsp_disable_adc_capture (struct snd_cs46xx *chip)
1807{
1808	struct dsp_spos_instance * ins = chip->dsp_spos_instance;
1809
1810	if (snd_BUG_ON(!ins->adc_input))
1811		return -EINVAL;
1812
1813	mutex_lock(&chip->spos_mutex);
1814	cs46xx_dsp_remove_scb (chip,ins->adc_input);
1815	ins->adc_input = NULL;
1816	mutex_unlock(&chip->spos_mutex);
1817
1818	return 0;
1819}
1820
1821int cs46xx_poke_via_dsp (struct snd_cs46xx *chip, u32 address, u32 data)
1822{
1823	u32 temp;
1824	int  i;
1825
1826	/* santiy check the parameters.  (These numbers are not 100% correct.  They are
1827	   a rough guess from looking at the controller spec.) */
1828	if (address < 0x8000 || address >= 0x9000)
1829		return -EINVAL;
1830        
1831	/* initialize the SP_IO_WRITE SCB with the data. */
1832	temp = ( address << 16 ) | ( address & 0x0000FFFF);   /* offset 0 <-- address2 : address1 */
1833
1834	snd_cs46xx_poke(chip,( SPIOWRITE_SCB_ADDR      << 2), temp);
1835	snd_cs46xx_poke(chip,((SPIOWRITE_SCB_ADDR + 1) << 2), data); /* offset 1 <-- data1 */
1836	snd_cs46xx_poke(chip,((SPIOWRITE_SCB_ADDR + 2) << 2), data); /* offset 1 <-- data2 */
1837    
1838	/* Poke this location to tell the task to start */
1839	snd_cs46xx_poke(chip,((SPIOWRITE_SCB_ADDR + 6) << 2), SPIOWRITE_SCB_ADDR << 0x10);
1840
1841	/* Verify that the task ran */
1842	for (i=0; i<25; i++) {
1843		udelay(125);
1844
1845		temp =  snd_cs46xx_peek(chip,((SPIOWRITE_SCB_ADDR + 6) << 2));
1846		if (temp == 0x00000000)
1847			break;
1848	}
1849
1850	if (i == 25) {
1851		dev_err(chip->card->dev,
1852			"dsp_spos: SPIOWriteTask not responding\n");
1853		return -EBUSY;
1854	}
1855
1856	return 0;
1857}
1858
1859int cs46xx_dsp_set_dac_volume (struct snd_cs46xx * chip, u16 left, u16 right)
1860{
1861	struct dsp_spos_instance * ins = chip->dsp_spos_instance;
1862	struct dsp_scb_descriptor * scb; 
1863
1864	mutex_lock(&chip->spos_mutex);
1865	
1866	/* main output */
1867	scb = ins->master_mix_scb->sub_list_ptr;
1868	while (scb != ins->the_null_scb) {
1869		cs46xx_dsp_scb_set_volume (chip,scb,left,right);
1870		scb = scb->next_scb_ptr;
1871	}
1872
1873	/* rear output */
1874	scb = ins->rear_mix_scb->sub_list_ptr;
1875	while (scb != ins->the_null_scb) {
1876		cs46xx_dsp_scb_set_volume (chip,scb,left,right);
1877		scb = scb->next_scb_ptr;
1878	}
1879
1880	ins->dac_volume_left = left;
1881	ins->dac_volume_right = right;
1882
1883	mutex_unlock(&chip->spos_mutex);
1884
1885	return 0;
1886}
1887
1888int cs46xx_dsp_set_iec958_volume (struct snd_cs46xx * chip, u16 left, u16 right)
1889{
1890	struct dsp_spos_instance * ins = chip->dsp_spos_instance;
1891
1892	mutex_lock(&chip->spos_mutex);
1893
1894	if (ins->asynch_rx_scb != NULL)
1895		cs46xx_dsp_scb_set_volume (chip,ins->asynch_rx_scb,
1896					   left,right);
1897
1898	ins->spdif_input_volume_left = left;
1899	ins->spdif_input_volume_right = right;
1900
1901	mutex_unlock(&chip->spos_mutex);
1902
1903	return 0;
1904}
1905
1906#ifdef CONFIG_PM_SLEEP
1907int cs46xx_dsp_resume(struct snd_cs46xx * chip)
1908{
1909	struct dsp_spos_instance * ins = chip->dsp_spos_instance;
1910	int i, err;
1911
1912	/* clear parameter, sample and code areas */
1913	snd_cs46xx_clear_BA1(chip, DSP_PARAMETER_BYTE_OFFSET,
1914			     DSP_PARAMETER_BYTE_SIZE);
1915	snd_cs46xx_clear_BA1(chip, DSP_SAMPLE_BYTE_OFFSET,
1916			     DSP_SAMPLE_BYTE_SIZE);
1917	snd_cs46xx_clear_BA1(chip, DSP_CODE_BYTE_OFFSET, DSP_CODE_BYTE_SIZE);
1918
1919	for (i = 0; i < ins->nmodules; i++) {
1920		struct dsp_module_desc *module = &ins->modules[i];
1921		struct dsp_segment_desc *seg;
1922		u32 doffset, dsize;
1923
1924		seg = get_segment_desc(module, SEGTYPE_SP_PARAMETER);
1925		err = dsp_load_parameter(chip, seg);
1926		if (err < 0)
1927			return err;
1928
1929		seg = get_segment_desc(module, SEGTYPE_SP_SAMPLE);
1930		err = dsp_load_sample(chip, seg);
1931		if (err < 0)
1932			return err;
1933
1934		seg = get_segment_desc(module, SEGTYPE_SP_PROGRAM);
1935		if (!seg)
1936			continue;
1937
1938		doffset = seg->offset * 4 + module->load_address * 4
1939			+ DSP_CODE_BYTE_OFFSET;
1940		dsize   = seg->size * 4;
1941		err = snd_cs46xx_download(chip,
1942					  ins->code.data + module->load_address,
1943					  doffset, dsize);
1944		if (err < 0)
1945			return err;
1946	}
1947
1948	for (i = 0; i < ins->ntask; i++) {
1949		struct dsp_task_descriptor *t = &ins->tasks[i];
1950		_dsp_create_task_tree(chip, t->data, t->address, t->size);
1951	}
1952
1953	for (i = 0; i < ins->nscb; i++) {
1954		struct dsp_scb_descriptor *s = &ins->scbs[i];
1955		if (s->deleted)
1956			continue;
1957		_dsp_create_scb(chip, s->data, s->address);
1958	}
1959	for (i = 0; i < ins->nscb; i++) {
1960		struct dsp_scb_descriptor *s = &ins->scbs[i];
1961		if (s->deleted)
1962			continue;
1963		if (s->updated)
1964			cs46xx_dsp_spos_update_scb(chip, s);
1965		if (s->volume_set)
1966			cs46xx_dsp_scb_set_volume(chip, s,
1967						  s->volume[0], s->volume[1]);
1968	}
1969	if (ins->spdif_status_out & DSP_SPDIF_STATUS_HW_ENABLED) {
1970		cs46xx_dsp_enable_spdif_hw(chip);
1971		snd_cs46xx_poke(chip, (ins->ref_snoop_scb->address + 2) << 2,
1972				(OUTPUT_SNOOP_BUFFER + 0x10) << 0x10);
1973		if (ins->spdif_status_out & DSP_SPDIF_STATUS_PLAYBACK_OPEN)
1974			cs46xx_poke_via_dsp(chip, SP_SPDOUT_CSUV,
1975					    ins->spdif_csuv_stream);
1976	}
1977	if (chip->dsp_spos_instance->spdif_status_in) {
1978		cs46xx_poke_via_dsp(chip, SP_ASER_COUNTDOWN, 0x80000005);
1979		cs46xx_poke_via_dsp(chip, SP_SPDIN_CONTROL, 0x800003ff);
1980	}
1981	return 0;
1982}
1983#endif