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