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