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